Results 1 to 14 of 14
Thread: Java Help Regular Expression
- 01-28-2012, 04:46 AM #1
Member
- Join Date
- Jan 2012
- Posts
- 12
- Rep Power
- 0
-
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
- 01-28-2012, 04:53 AM #3
Member
- Join Date
- Jan 2012
- Posts
- 12
- Rep Power
- 0
Re: Java Help Regular Expression
I was thinking:
regex = "\w{1,}\s{0,1}\w{1,}" but I keep getting illegal escape character
- 01-28-2012, 04:57 AM #4
Member
- Join Date
- Jan 2012
- Posts
- 12
- Rep Power
- 0
Re: Java Help Regular Expression
Since it's only letters it's ^[A-Z-a-z]
-
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...
- 01-28-2012, 05:18 AM #6
Member
- Join Date
- Jan 2012
- Posts
- 12
- Rep Power
- 0
Re: Java Help Regular Expression
Yeah, this one isn't easy.
-
Re: Java Help Regular Expression
OK, I figured it out. Not that hard actually.
-
Re: Java Help Regular Expression
What do you think should be the first term of the regex?
- 01-28-2012, 05:33 AM #9
Member
- Join Date
- Jan 2012
- Posts
- 12
- Rep Power
- 0
Re: Java Help Regular Expression
Something near "^[\p{Z,1}][\w*]^[\p{Z,1]"
-
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.
- 01-28-2012, 05:50 AM #11
Member
- Join Date
- Jan 2012
- Posts
- 12
- Rep Power
- 0
Re: Java Help Regular Expression
Something near "^[\\w*]\\s{0,1}[\\w*]"
-
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.
- 01-28-2012, 06:23 AM #13
Member
- Join Date
- Jan 2012
- Posts
- 12
- Rep Power
- 0
Re: Java Help Regular Expression
Thanks alot
- 01-28-2012, 08:29 AM #14
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.
Assuming you're using matches() (not find())That literally says: one or more alpha characters, followed by zero or one space, followed by one or more alpha characters.Java Code:"\\p{Alpha}+ {0,1}\\p{Alpha}+"
dbWhy do they call it rush hour when nothing moves? - Robin Williams
Similar Threads
-
URGENT HELP : Java Regular Expression
By asheshrocky in forum Advanced JavaReplies: 3Last Post: 12-07-2011, 02:52 PM -
Java regular expression optimization - help needed
By dpkcv in forum Advanced JavaReplies: 0Last Post: 09-30-2011, 08:42 AM -
Java Regular expression ?
By sidharth in forum Advanced JavaReplies: 12Last Post: 11-14-2009, 11:09 AM -
Java Regular Expression help
By royalibrahim in forum Advanced JavaReplies: 11Last Post: 11-12-2009, 01:27 AM -
java regular expression to search block/string/word exist in a paragraph?
By sidharth in forum Advanced JavaReplies: 2Last Post: 11-11-2009, 05:56 AM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks