[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

How to improve my code to perform something if the match is exactly the way I want in JAVA regular expression?

Posted on 2011-10-14
9
Medium Priority
?
260 Views
Last Modified: 2012-06-21
Hi,
I have the following code in JAVA:

java.lang.String IDListLink = IDList.replaceAll("(\\d+)", "g$1");

Open in new window


What this does is:

It adds g in front of the digits.

Lets say I have this:

111111,222222,3333333

then it becomes

g111111,g222222,g333333


How can I change this code in way that it first checks if there is g infront of the digit and if there is no g prefix then add it to the digits as a prefix? But I want to do it in one line by using the power of regular expressions. I don't wanna add any if or for loop.

So if I have this:

111111,g222222,333333

I want to see:

g111111,g222222,g333333

but I don't want to see

g111111,gg222222,g333333

or

Does this code already do it?

Thanks,
0
Comment
Question by:Tolgar
  • 7
  • 2
9 Comments
 
LVL 47

Expert Comment

by:for_yan
ID: 36969505
I think negative look behind should worK:
java.lang.String IDListLink = IDList.replaceAll("(?<!g)(\\d+)", "g$1");

Open in new window


Let me test.
0
 
LVL 47

Accepted Solution

by:
for_yan earned 2000 total points
ID: 36969609
Looka-ahead for some reason did not work, but this works:

        String IDListLink =  "111111,g222222,333333";



      IDListLink = IDListLink.replaceAll("g([\\d]+)|([\\d]+)", "g$1$2");

                        System.out.println(IDListLink);

Open in new window


Output:
g111111,g222222,g333333

Open in new window

0
 
LVL 47

Expert Comment

by:for_yan
ID: 36969787
This is another way:

                           IDListLink =  "g111111,g222222,333333";
                       IDListLink = IDListLink.replaceAll("([\\s,]|^)(\\d+)", "$1g$2");

                        System.out.println("another way: " + IDListLink);

Open in new window


another way: g111111,g222222,g333333

Open in new window

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:Tolgar
ID: 36969851
@for_yan: The first one is more readable but I wonder how you read the second one:

.replaceAll("([\\s,]|^)(\\d+)", "$1g$2");

Open in new window



What does each term represent in here?

Thanks,
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36969868
So it matches space or comma or eginning of the string in the first part and then digits in the second part and insterts g between them, so
if you jhhave ",g222" - it will not match it and not insert any more g's, but this one ",333" it will maych and insert g after comma
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36969892
I could not make negative lookbehind working, as it was matching the first charcter, which does not preced by g,
so in g222222 - the second "2" was matching and it was becoming g2g2222 -and with this negative look behind
I could not overcome it
0
 

Author Comment

by:Tolgar
ID: 36969898
ok. THen I will go with the first one because it also cover ",g222" case I  guess.

Am I right?

Thanks,

0
 
LVL 47

Expert Comment

by:for_yan
ID: 36969910
I think they both worked - at least with cases I tested them like 11111,g22222,3333  and g1111,g22222.33333
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36971967
This is finally with look-behind
However with positive rather than negative look-behind

negative all the time matches the last 5 digits of say g222222,
so it becomes g2g22222.

with positive, though, it ssesm to be working, as below:

                     IDListLink =  "g111111, 222222, g333333";
           IDListLink = IDListLink.replaceAll("((?<=[\\s,]|^)[\\d]+)", "g$0");
             System.out.println("still trying look behind: " + IDListLink);

Open in new window


Output:
still trying look behind: g111111, g222222, g333333

Open in new window


So this will match (and add "g" in front)
all sequences of digits [\\d]+ which
will have either comma or space  [\\s,] or beginning of the whole string  ^ right before these digits
0

Featured Post

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!

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses
Course of the Month18 days, 9 hours left to enroll

826 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