Regex help....

Joined
Mar 9, 2004
Messages
9,065
Reaction score
331
Points
83
Location
Munich
Your Mac's Specs
Aluminium Macbook 2.4 Ghz 4GB RAM, SSD 24" Samsung Display, iPhone 4, iPad 2
Hey guys!

I'm working on a uni project and am having trouble parsing a string using regex. (this is a Java project)

Example:
"@John23 This is my message".

I want to be able to recognize "@John23", so I can route the message only to the user "John23".

I can get it working without numerals using this code as the regex input strings:

Code:
String re1="(@)";	// PM Identifier
String re2="((?:[a-z][a-z]+))";	// Username
String re3="(\\s+)";	// White Space = separator username / message

But I've been having trouble getting it working with any type of ASCII character as the username. I think it's as simple as extending the "re2" String to include 0-9 etc.
Or making "re2" accept any characters, except whitespace (I think ".*" would also include whitespace, correct?).

Any ideas?
 
Joined
Jun 25, 2005
Messages
3,231
Reaction score
112
Points
63
Location
On the road
Your Mac's Specs
2011 MBP, i7, 16GB RAM, MBP 2.16Ghz Core Duo, 2GB ram, Dual 867Mhz MDD, 1.75GB ram, ATI 9800 Pro vid
Lesson: Regular Expressions and Using Regular Expressions in Java may be of value to you.

I'm not sure, but I think you want;
Code:
"((?:[a-zA-Z0-9]+))"

Why is the question mark in there by it self? This is the only thing I could find about it in one of those links; "Groups beginning with (? are pure, non-capturing groups that do not capture text and do not count towards the group total."
 
OP
Aptmunich
Joined
Mar 9, 2004
Messages
9,065
Reaction score
331
Points
83
Location
Munich
Your Mac's Specs
Aluminium Macbook 2.4 Ghz 4GB RAM, SSD 24" Samsung Display, iPhone 4, iPad 2
Lesson: Regular Expressions and Using Regular Expressions in Java may be of value to you.

I'm not sure, but I think you want;
Code:
"((?:[a-zA-Z0-9]+))"

Why is the question mark in there by it self? This is the only thing I could find about it in one of those links; "Groups beginning with (? are pure, non-capturing groups that do not capture text and do not count towards the group total."
Cheers!

The "?" is there because that's what the regex generator website I used spit back :)

txt2re: headache relief for programmers :: regular expression generator
 
Joined
Jun 25, 2005
Messages
3,231
Reaction score
112
Points
63
Location
On the road
Your Mac's Specs
2011 MBP, i7, 16GB RAM, MBP 2.16Ghz Core Duo, 2GB ram, Dual 867Mhz MDD, 1.75GB ram, ATI 9800 Pro vid
Thanks for the site. I book marked it. :)
 
OP
Aptmunich
Joined
Mar 9, 2004
Messages
9,065
Reaction score
331
Points
83
Location
Munich
Your Mac's Specs
Aluminium Macbook 2.4 Ghz 4GB RAM, SSD 24" Samsung Display, iPhone 4, iPad 2
Thanks for the site. I book marked it. :)
Yeah, it's quite useful - especially if you don't know much about Regex at all like me :)

Cheers for the help, it worked a treat!
 

Shop Amazon


Shop for your Apple, Mac, iPhone and other computer products on Amazon.
We are a participant in the Amazon Services LLC Associates Program, an affiliate program designed to provide a means for us to earn fees by linking to Amazon and affiliated sites.
Top