Link to home
Start Free TrialLog in
Avatar of michaelm702
michaelm702

asked on

regular exp

I have a regular expression: "(John) (.+?) "
And String: "John writes about this, and John Doe writes about that, and John Wayne writes about everything."
So I get the following result:
John writes
John Doe
John Wayne

Now I have a another string from user which specifies what needs to be done with the matched strings so for e.g.
%1-%2-%3
%3.%2
[%2]-[%3]-[%1]

So which means I need to replace the %1, %2, %3 etc.. matches with the actual matched items i.e.:
%1-%2-%3   ==== John writes-John Doe-John Wayne
...

How can I do this in Java? The second string which we need to get the matched result into can be of any type but will have the number %1,%2,%3... which we need to replace with the reg ex matches.

I have below code till now:
String covertTo="%1-%2-%3";
        String text    =
                  "John writes about this, and John Doe writes about that," +
                          " and John Wayne writes about everything."
                ;

        String patternString1 = "(John) (.+?) ";

        Pattern pattern = Pattern.compile(patternString1);
        Matcher matcher = pattern.matcher(text);

        while (matcher.find()) {      
            System.out.print("Start index: " + matcher.start());
            System.out.print(" End index: " + matcher.end() + " ");
            System.out.println("group: "+matcher.group());
          }

        //Need to write code to store the above matches into array and replace as required and output to user
Avatar of Tomas Helgi Johannsson
Tomas Helgi Johannsson
Flag of Iceland image

Hi!

Use the matcher.group() and matcher.groupCount() to loop the results and put into
an array
http://www.java2s.com/Code/Java/Regular-Expressions/Matcher.htm

Regards,
      Tomas Helgi
So I get the following result:
Ummm, you shouldn't. You haven't really anchored that dot-star, so even though you made it non-greedy, it's still going to match up to the end of the string. Furthermore, you show only two capture groups, but you show 3 replacement backreferences. Is that the actual pattern?
Avatar of michaelm702
michaelm702

ASKER

actually on trying to go through tutorial for reg ex I was able to find there is a functionality in matcher that allows me to do that:
    String input = "Customer Account Number";
       String regexe = "([a-z]+) ([a-z]+) ([a-z]+)"; // pattern to be matched
       String replacement = "$3 and $2-$1";    // replacement pattern with back references
   
       // Step 1: Allocate a Pattern object to compile a regexe
       pattern = Pattern.compile(regexe, Pattern.CASE_INSENSITIVE);
   
       // Step 2: Allocate a Matcher object from the Pattern, and provide the input
       matcher = pattern.matcher(input);
   
       // Step 3: Perform the matching and process the matching result
       String output = matcher.replaceAll(replacement);     // all matches
       System.out.println("all: "+output);

       output = matcher.replaceFirst(replacement); // first match only
       System.out.println("first: "+output);
This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.