File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
George Cui
Greenhorn
Posts: 5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Pie
Posts: 20372
44
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 $.
 
George Cui
Greenhorn
Posts: 5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20372
44
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks so much Rob. It works with ".*[^\r\n]"
What a great site for asking for help!
 
Rob Spoor
Sheriff
Pie
Posts: 20372
44
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic