Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java regex find match between commas

Tags:

java

regex

I am trying to find a match between commas if it contains a specific string.

so far i have ,(.*?myString.?*),

Obviously this finds all the input between the first comma in the entire input and the first comma after the string i want. How do i reference the comma immediately before the string that i want?

Edit: i also want to find the match that occurs after a specific set of characters

ie. occurs after (fooo)

dsfdsdafd,safdsa,gfdsgdtheMatchfdsgfd,dsafdsa,dsfoooafd,safdsa,gfhhhgdtheMatchfhhhfd,dsafdsa

returns gfhhhgdtheMatchfhhhfd, not gfdsgdtheMatchfdsgfd

like image 547
Ben Arnao Avatar asked May 27 '26 06:05

Ben Arnao


2 Answers

The following regex should do it :

[^,]+theMatch.*?(?=,)

see regex demo / explanation

Java ( demo )

import java.util.regex.Matcher;
import java.util.regex.Pattern;

class RegEx {
    public static void main(String[] args) {
        String s = "dsfdsdafd,safdsa,gfdsgdtheMatchfdsgfd,dsafdsa";
        String r = "[^,]+theMatch.*?(?=,)";
        Pattern p = Pattern.compile(r);
        Matcher m = p.matcher(s);
        while (m.find()) {
            System.out.println(m.group());  // gfdsgdtheMatchfdsgfd
        }
    }
}

Edit

use this regex fooo.*?([^,]+theMatch.*?)(?=,) demo

like image 179
m87 Avatar answered Jun 01 '26 12:06

m87


You are finding too much because .* will include the comma.

You need the following regular expression: ,([^,]*myinput[^,]*),

[^,]* basically says find all non-comma characters.

I would suggest the following code:

import java.util.regex.*;

public class Main {

    public static void main(String[] args) {

        String str = "dsfdsdafd,safdsa,myinput,dsafdsa";

        Pattern p = Pattern.compile(",([^,]*myinput[^,]*),");
        Matcher m = p.matcher(str);

        if(m.find()) {

            System.out.println(m.group(0));
            // prints out ",myinput,"

            System.out.println(m.group(1));
            // prints out "myinput"
        }

    }
}

Here is a StackOverflow question that is basically the same with some very good answers associated: Regex to find internal match between two characters

For more on regular expressions in Java look here: http://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html

If you want the position of the comma proceeding your input string use the following code:

import java.util.regex.*;

public class Main {

    public static void main(String[] args) {

        String str = "dsfdsdafd,safdsa,myinput,dsafdsa";

        Pattern p = Pattern.compile(",([^,]*myinput[^,]*),");
        Matcher m = p.matcher(str);

        if(m.find()) {

            System.out.println(str.indexOf(m.group(0)));

            // prints out "16"
        }

    }
}

By feeding the match of the regular expression into the String Method indexOf( you are able to locate the position of the start of your string.

Edit:

To find the occurrence of a string following another string, simply modify the regex to: fooo.*,([^,]*theMatch[^,]*),

fooo.* will greedily consume all characters between fooo and the start of your match.

Example code:

import java.util.regex.*;

public class Main {

    public static void main(String[] args) {

        String str = "dsfdsdafd,safdsa,gfdsgdtheMatchfdsgfd,dsafdsa,dsfoooafd,safdsa,gfhhhgdtheMatchfhhhfd,dsafdsa";

        Pattern p = Pattern.compile("fooo.*,([^,]*theMatch[^,]*),");
        Matcher m = p.matcher(str);

        if(m.find()) {

            System.out.println(m.group(1));
            // prints out: gfhhhgdtheMatchfhhhfd
        }

    }
}
like image 41
Micah Akin Avatar answered Jun 01 '26 11:06

Micah Akin