Results 1 to 14 of 14
Like Tree1Likes
  • 1 Post By DarrylBurke

Thread: Java Help Regular Expression

  1. #1
    geforce is offline Member
    Join Date
    Jan 2012
    Posts
    12
    Rep Power
    0

    Default Java Help Regular Expression

    "Only letters allowed and at most one blank, which cannot be the first or last character"
    What regular expression would meet this and why?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Java Help Regular Expression

    How about giving it a try and showing us your attempt? If you need help, please check these links on regular expressions:

    Lesson: Regular Expressions: for the basic concepts
    Regex Tutorial: for more advanced concepts

  3. #3
    geforce is offline Member
    Join Date
    Jan 2012
    Posts
    12
    Rep Power
    0

    Default Re: Java Help Regular Expression

    I was thinking:

    regex = "\w{1,}\s{0,1}\w{1,}" but I keep getting illegal escape character

  4. #4
    geforce is offline Member
    Join Date
    Jan 2012
    Posts
    12
    Rep Power
    0

    Default Re: Java Help Regular Expression

    Since it's only letters it's ^[A-Z-a-z]

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Java Help Regular Expression

    In Java, your backslashes need to be escaped -- "\\w" not "\w". But even so, your regex won't work. I honestly don't know the solution, but am trying to work on it myself...

  6. #6
    geforce is offline Member
    Join Date
    Jan 2012
    Posts
    12
    Rep Power
    0

    Default Re: Java Help Regular Expression

    Yeah, this one isn't easy.

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Java Help Regular Expression

    What do you think should be the first term of the regex?

  9. #9
    geforce is offline Member
    Join Date
    Jan 2012
    Posts
    12
    Rep Power
    0

    Default Re: Java Help Regular Expression

    Something near "^[\p{Z,1}][\w*]^[\p{Z,1]"

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Java Help Regular Expression

    No, you're doing what I was doing -- making things too complicated; it's much simpler than this.

    The first bit of the regex, the start should be "\\w", that's it. Then after this consider using a vertical bar or pipe to allow the regex to accept one of two following Strings -- either a String made of only characters, or a String with a variable number of characters, a space, and followed by a variable number of characters.

    So the regex will look something like "\\w(...|...)" where you would have to fill in the ... parts.

  11. #11
    geforce is offline Member
    Join Date
    Jan 2012
    Posts
    12
    Rep Power
    0

    Default Re: Java Help Regular Expression

    Something near "^[\\w*]\\s{0,1}[\\w*]"

  12. #12
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Java Help Regular Expression

    That idea can work too, but don't forget that first \\w that must be present. Your code would allow a String that starts with a space, and still allows any white-space character, not just a space.

  13. #13
    geforce is offline Member
    Join Date
    Jan 2012
    Posts
    12
    Rep Power
    0

    Default Re: Java Help Regular Expression

    Thanks alot

  14. #14
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,236
    Rep Power
    19

    Default Re: Java Help Regular Expression

    The 'word' character \\w is equivalent to [a-zA-Z_0-9] which allows digits and underscore in addition to alpha characters.

    Quote Originally Posted by geforce View Post
    "Only letters allowed and at most one blank, which cannot be the first or last character"
    What regular expression would meet this and why?
    Assuming you're using matches() (not find())
    Java Code:
    "\\p{Alpha}+ {0,1}\\p{Alpha}+"
    That literally says: one or more alpha characters, followed by zero or one space, followed by one or more alpha characters.

    db
    Fubarable likes this.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. URGENT HELP : Java Regular Expression
    By asheshrocky in forum Advanced Java
    Replies: 3
    Last Post: 12-07-2011, 02:52 PM
  2. Java regular expression optimization - help needed
    By dpkcv in forum Advanced Java
    Replies: 0
    Last Post: 09-30-2011, 08:42 AM
  3. Java Regular expression ?
    By sidharth in forum Advanced Java
    Replies: 12
    Last Post: 11-14-2009, 11:09 AM
  4. Java Regular Expression help
    By royalibrahim in forum Advanced Java
    Replies: 11
    Last Post: 11-12-2009, 01:27 AM
  5. Replies: 2
    Last Post: 11-11-2009, 05:56 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •