• Post Reply Bookmark Topic Watch Topic
  • New Topic

Regular expression question  RSS feed

 
John La
Greenhorn
Posts: 12
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to use a regular expression to match "word1" [white-space] "word2". I tried various forms similar to this:



but alas, the result is false.

Thanks in advance for any help you may be able to provide.

 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John La wrote:I'm trying to use a regular expression to match "word1" [white-space] "word2". I tried various forms similar to this:



but alas, the result is false.

Thanks in advance for any help you may be able to provide.



First, there is no reason for the {1} qualifiers. Any character or group that doesn't have a qualifier is, by default, has to match exactly once.

Second, there is no reason for the non-capturing groups either.

And finally, it should work. Just to be sure, I just tried it. It returned true.

Henry
 
John La
Greenhorn
Posts: 12
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As a matter of fact, it does. (Hmmm...)

My example was lost in the translation. It more correctly should be:



which returns false , much to my surprise.

I'm using JRE 1.5.0_14.
 
John La
Greenhorn
Posts: 12
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A case of RTFM, I'm afraid. "." may or may-not match line terminators. The default is to NOT match line terimators.

This works as I had wished:


The "(?s)" expression enables DOTALL mode, in which line terminators are matched by ".".

Thanks for the help, sometimes the question is answered by asking it.
 
Richard Tookey
Bartender
Posts: 1166
17
Java Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John La wrote:
This works as I had wished:


But as Henry has pointed out you don't need the {1} qualifiers or the capturing or the non-capturing groups so the regex is far far more complex than it needs to be!
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John La wrote:A case of RTFM, I'm afraid. "." may or may-not match line terminators. The default is to NOT match line terimators.

This works as I had wished:


The "(?s)" expression enables DOTALL mode, in which line terminators are matched by ".".

Thanks for the help, sometimes the question is answered by asking it.



Yeah, sometimes just explaining the problem solves it... it happens a lot for me, which explains why I always seem to talk to myself ...

Anyway, glad that you solved it. And also, thank you for coming back here, to give us the very detailed answer. Most people just abandon their topics once the solution is found.,,. I just awarded you a cow.

Henry
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!