This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Java in General and the fly likes Java regexp Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Java regexp" Watch "Java regexp" New topic
Author

Java regexp

nag ptl
Greenhorn

Joined: Aug 05, 2008
Posts: 4
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

Joined: Jul 30, 2008
Posts: 162

test this



Thanks
Piet Verdriet
Ranch Hand

Joined: Feb 25, 2006
Posts: 266
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

Joined: Jul 30, 2008
Posts: 162

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

Joined: Feb 25, 2006
Posts: 266
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

Joined: Feb 25, 2006
Posts: 266
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

Joined: Jul 30, 2008
Posts: 162

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

Joined: Feb 25, 2006
Posts: 266
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

Joined: Aug 05, 2008
Posts: 4
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

Joined: Feb 25, 2006
Posts: 266
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

Joined: Aug 05, 2008
Posts: 4
Oops..please remove the 'final' keyword from 'test' variable declaration. The problem remains the same.

Thanks,
-np
nag ptl
Greenhorn

Joined: Aug 05, 2008
Posts: 4
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

Joined: Feb 25, 2006
Posts: 266
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

Joined: Oct 27, 2005
Posts: 19655
    
  18

nag ptl,

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

Thank you,

Rob


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Bill Shirley
Ranch Hand

Joined: Nov 08, 2007
Posts: 457
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.


Bill Shirley - bshirley - frazerbilt.com
if (Posts < 30) you.read( JavaRanchFAQ);
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19655
    
  18

I agree. I was just thinking of lastIndexOf, substring and concatenation.
 
 
subject: Java regexp
 
Similar Threads
Newbie to JavaScript
Regular Expression to check String is valid XHTML or not
Treemap and Hashmap
To the author
Regular Expressions