Jump to content

Comparing or matching strings.

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
4 replies to this topic

#1
ns_uni

ns_uni

    Newbie

  • Members
  • PipPip
  • 15 posts
Hi,

I have a mostly functioning program (for uni) but I can not seem to get one module to work.

- I want to read in a String from the keyboard. (I know how to do this).
- I need to then make sure the string is only a word, so I want to do a regex check to see if it contains any punctuation or anything but [a-z]. And return a boolean expression (this I dont know how to do, well o can't get it to work).
- I want to then to have a DOWHILE loop to repeat until the entry is valid (only a-z characters)

I have read online about using the regex [^\w] or \p{Punct} but I don't know how to make it work..

I am new to java so be nice please and also make the steps very clear, I am new to classes as well.

Ok Thanks..

ND

#2
ns_uni

ns_uni

    Newbie

  • Members
  • PipPip
  • 15 posts
Whoops the title should have been called comparing a string to a regex..

HELP!

#3
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
I think you want "^[\w]*$"
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#4
ns_uni

ns_uni

    Newbie

  • Members
  • PipPip
  • 15 posts
Ok scrap that...

I need to check a string to see if it contains any punctuation characters.. if it does i need to return a boolean ...

The best way to do this is?

Anyone??

#5
brightmatter

brightmatter

    Learning Programmer

  • Members
  • PipPipPip
  • 36 posts
Go here:

gskinner
.
com
/
RegExr
/

I have to break up the link because I don't post enough to write a true link in my comments, apparently.

anyway, there are two things here, are you looking for non-letters and numbers? "[^1-9a-zA-Z ]"? or are you looking for sentence enders "[?.!]*"? oh, and are you looking to return the very first one you fine (if any) or all of them?