The Artima Developer Community
Sponsored Link

Java Answers Forum
regexp problem - smallest possible match on string

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Adam W

Posts: 1
Nickname: g00dlife
Registered: Mar, 2003

regexp problem - smallest possible match on string Posted: Mar 11, 2003 7:58 PM
Reply to this message Reply
Advertisement
I need some help on simple regular expression.

Just say I have a string for example:

String str = "start start start match1 finish finish finish";

I would like to extract the smallest string that is bounded by "start" and "finish"... ie. in this case I would like to extract the string "match1".

My first guess was to use:

pattern = "start (.*?) finish";

however this matches the string "start start match1". (This behaviour is identical to using the same string and pattern with Perl5).

I know I can use the pattern:

pattern = ".*start (.*?) finish";

and get what I want, however there's a performance issue by using the greedy matcher .* at the start of the pattern. (This performance issue is specifically with Jakarta ORO, however I've tried GNU Regexp and has similar performance issues by using multiple greedy and non-greedy matchers in my regexp patterns.)

Is there another single regular expression that will extract "match1" from the above $str when you only know it's bounded by "start" and "finish".

Any help would be greatly appreciated.

Topic: How validate Schema with JAXP DOM? Previous Topic   Next Topic Topic: JView.exe

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use