wood burning stoves 2.0*
The moose likes Java in General and the fly likes Java regex search. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Java regex search. ".*"  macther.find() or matcher.replaceAll, give more matches than intended" Watch "Java regex search. ".*"  macther.find() or matcher.replaceAll, give more matches than intended" New topic
Author

Java regex search. ".*" macther.find() or matcher.replaceAll, give more matches than intended

George Cui
Greenhorn

Joined: Dec 10, 2011
Posts: 5

I got results:
This 1 is line one
This 2 is line two
This 3 is line three
This 4 is line Four
1 This 1 is line one
2
3
4 This 2 is line two
5
6
7 This 3 is line three
8
9
10 This 4 is line Four
11
ReplaceAll:
XXXXXX
XXX
XXXXXX
XXX
XXXXXX
XXX
XXXXXX
replace first :XxX
This 2 is line two
This 3 is line three
This 4 is line Four

That means if I use ".*", a single line will have 2 matches (try just one line case ) and another find is the line.separator. If I pass that, it will replace lines that I do not intended to replace. I just want
XXX
XXX
XXX
XXX

If the search pattern flawed or I should do something in the code to skip the 2 additional XXX (which I prefer not to do any manipulation to change the meaning of the search)
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19685
    
  20

Add Pattern.DOTALL to your flags: Pattern.MULTILINE | Pattern.DOTALL. Without it, . does not match the line breaks. Pattern.MULTILINE only changes the treatment of ^ and $.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
George Cui
Greenhorn

Joined: Dec 10, 2011
Posts: 5
Thanks for all the quick reponses. Sorry that it was my very first post and did not know how to format it in the page.
I tried to use
Pattern.MULTILINE | Pattern.DOTALL, it gives me this

This 1 is line one
This 2 is line two
This 3 is line three
This 4 is line Four
1 This 1 is line one
This 2 is line two
This 3 is line three
This 4 is line Four
2
ReplaceAll:
XXXXXX
replace first :XxX

It treat the whole StringBuffer as one big line (with 2 matches in 2 replacements resulting in XXXXXX), I want to search such that replaveAll will get me
XXX
XXX
XXX
instead of XXXXXX
Is my search pattern wrong or is something else is not used properly
Thanks -George
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19685
    
  20

So you want to replace everything but the line breaks with XXX? Then don't use . but explicitly say "everything but line breaks": [^\r\n].
George Cui
Greenhorn

Joined: Dec 10, 2011
Posts: 5
Thanks so much Rob. It works with ".*[^\r\n]"
What a great site for asking for help!
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19685
    
  20

You're welcome.
 
 
subject: Java regex search. ".*" macther.find() or matcher.replaceAll, give more matches than intended