Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Regex string matching question

Posted on 2006-06-08
7
Medium Priority
?
250 Views
Last Modified: 2010-03-31
Hi,
If I want to replacethe string 'start abcdefg 12345 6blahblahblah end' using a regex, where  that string might in in much larger string I can use

Pattern p = Pattern.compile(''start .+end");
Matcher m = p.matcher(huge_chunk_of_text);
     
String changedInput =  m.replaceAll(replacementText);


This works fine if the string I'm looking for only occurs once in huge_chunk_of_text but if it occurs  more than once then the regex i'm using,'start .+end" will only fine one match, eg  'start abcdefg 12345 6blahblahblah end blah blah start abcdefg 12345 6blahblahblah end'' should be two matches not one. How do I specify that there should not be another 'start' before 'end', I have tried using [start][+.][^start][end] but that doesn't work.
0
Comment
Question by:mikehalfsheen
  • 3
  • 2
  • 2
7 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 16861566
You need to call matcher.find() in a loop
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 1600 total points
ID: 16861588
or more simply use replaceAll(''start .+?end", "replacement");
0
 
LVL 14

Assisted Solution

by:hoomanv
hoomanv earned 400 total points
ID: 16861840
in addition to CEHJ's comment
you should know how various quantifiers (+? instead of + in your case) results in different pattern matching
http://java.sun.com/docs/books/tutorial/extra/regex/quant.html
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:mikehalfsheen
ID: 16861914
Ta, I still haven't cracked it. The replaceAll(''start .+?end", "replacement"); works, ie finds 2 occurrences when the input is
'start abcdefg 12345 6blahblahblah end blah blah start abcdefg 12345 6blahblahblah end'

but doesn't work for 'start abcdefg 12345 6blahblahblah end start abcdefg 12345 6blahblahblah end some other text in here and then a bit more start abcdefg 12345 6blahblahblah end', in this case it just finds the one occurrence instead of three.
0
 
LVL 14

Expert Comment

by:hoomanv
ID: 16861955
no it finds all 3 occurrences, at least for me
0
 

Author Comment

by:mikehalfsheen
ID: 16862080
ooops. yes it does sorry, I'd screwed my test up :(
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16862194
:-)
0

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses
Course of the Month11 days, 4 hours left to enroll

572 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question