• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Regular expression question

 
Greenhorn
Posts: 16
1
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 16
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 16
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 1166
17
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Something must be done about this. Let's start by reading this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic