• 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

Regex reluctant mode problem

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all!

I have trouble with reluctant mode. There is my code:


Unfortunately, I can't make this pattern to work properly. If I change pattern from "<b>(.*)</b>.*?(?:<a href=\"(.*?)\")?" to "<b>(.*)</b>.*?(?:<a href=\"(.*?)\")+" this works. But this url pattern is optional here.

Please advise me how I should change the expression?

Thanks a lot!
 
Ranch Hand
Posts: 262
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Make the first dot-star reluctant, too:
 
Roman Tomach
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you! Sorry, I forgot to write this in my post.
If I add "?" a have the following result:

But I need this result:
 
Alan Moore
Ranch Hand
Posts: 262
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh, right: the second .*? initially matches nothing, then the rest of the expression tries to match at the same position. It fails, but that's okay because it's optional. So you get a successful match at every <B> element, no matter what follows it, and group #2 never matches anything.

I still don't understand what's supposed to happen, though. Can you give us some more information about what you're trying to do?
 
Roman Tomach
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Alan Moore:
I still don't understand what's supposed to happen, though. Can you give us some more information about what you're trying to do?


All I need is to make this expression to match both

and

because link here is optional.
 
Alan Moore
Ranch Hand
Posts: 262
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay, try this regex:
 
Roman Tomach
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Alan Moore:
Okay, try this regex:


Excellent! This is exactly what I need. Thank you very much!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic