Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java regexp

 
nag ptl
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi There,
can anyone provide a regular expression for replacing all the chars before '?' with 'aa' in the string 'xx.xx?yuyu'. Basically, I'm trying to use this regexp in replaceall(regexp,replacement) statement.

Thanks in advance for your help.

-np
 
Kuladip Yadav
Ranch Hand
Posts: 162
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
test this



Thanks
 
Piet Verdriet
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The previous reply is not quite correct, inside a character class, the '?' doesn't need to be escaped.
Here's a way:

 
Kuladip Yadav
Ranch Hand
Posts: 162
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Piet ,
You are right with code



But I am not getting significance of +.
Can we try like


I think that is more accurate.

What do you think ?

Thanks
 
Piet Verdriet
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Kuldeep Yadav:
Hi Piet ,
You are right with code



But I am not getting significance of +.
Can we try like


I think that is more accurate.

What do you think ?

Thanks


No, it's not more accurate. Removing the '+' will also be correct, but it will make the regex slower. By adding the '+', you make the [^?]* possessive and thus faster. Although for small strings, the difference will not be noticeable.

More information on possessive quantifiers:
http://www.regular-expressions.info/possessive.html
 
Piet Verdriet
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's a small benchmark:

It is clear that when working with larger Strings, making (parts) of your regex will enhance the performance greatly.
 
Kuladip Yadav
Ranch Hand
Posts: 162
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Piet Verdriet:
Here's a small benchmark:

It is clear that when working with larger Strings, making (parts) of your regex will enhance the performance greatly.



Thats perfect. Now I came to know its significance and efficiency
Thanks for your guidance.
 
Piet Verdriet
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Kuldeep Yadav:



Thats perfect. Now I came to know its significance and efficiency
Thanks for your guidance.



You're welcome, Kuldeep!
 
nag ptl
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks very much for clarification Piet and Kuldeep!

I don't find any fault with the expression. But for some reason, it doesn't seem to work. Tested code is as follows:

final String test = "xx.xx?yuyu";
test.replaceAll("[^?]*+[?]", "");

(test string remains the same..)

I tried "[^?]*[?]" too.

-np
 
Piet Verdriet
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is because String's are immutable, so by calling replaceAll(...) the actual String is NOT changed, but a new String is returned.
Do it like this:
 
nag ptl
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oops..please remove the 'final' keyword from 'test' variable declaration. The problem remains the same.

Thanks,
-np
 
nag ptl
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please ignore my previous message.

Arghh...looks like i need to revisit my java basics

Thanks for your time Piet...it works now.

-np
 
Piet Verdriet
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by nag ptl:
Please ignore my previous message.

Arghh...looks like i need to revisit my java basics

Thanks for your time Piet...it works now.

-np


No problem, glad I could help.
 
Rob Spoor
Sheriff
Pie
Posts: 20550
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nag ptl,

your name violates our naming policy. Please change it accordingly.

Thank you,

Rob
 
Bill Shirley
Ranch Hand
Posts: 457
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are, of course, MUCH more efficient ways of doing this specific thing without regex. If you're looking for a general solution, or trying to learn regex, that's great.

But If you want a faster computational way to solve the problem at the cost of more lines of code, there are those solutions out there.
 
Rob Spoor
Sheriff
Pie
Posts: 20550
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree. I was just thinking of lastIndexOf, substring and concatenation.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic