Sandeep Singh
Sandeep Singh

Reputation: 77

Regex to find Integers in particular string lines

I have this regex to find integers in a string (newlines). However, I want to filtrate this. I want the regex to find the number in certain lines, and not others.

String:

 String test= "ytrt.ytrwyt.ytreytre.test1,0,2,0"
+"sfgtr.ytyer.qdfre.uyeyrt.test2,0,8,0"
+"sfgtr.ytyer.qdfre.uyeyrt.test3,0,3,0";



pattern = "(?<=,)\\d+";

    pr = Pattern.compile(pattern);

    match = pr.matcher(test);
    System.out.println();
    if (match.find()) {


        System.out.println("Found: " + match.group());

    }

This regex find the integers after the comma, for all the lines. If I want a particular regex to find the integers in the line containing "test1", "test2", and "test3". How should I do this? I want to create three different regex, but my regex skills are weak.

First regex should print out 2. The second 8 and the third 3.

Upvotes: 0

Views: 104

Answers (3)

fabian
fabian

Reputation: 82461

You could also use capturing groups to extract the test number and the other number from the string:

String pattern = "test([123]),\\d+,(\\d+),";

...

while (match.find()) {
    // get and parse the number after "test" (first capturing group)
    int testNo = Integer.parseInt(match.group(1));
    // get and parse the number you wanted to extract (second capturing group)
    int num = Integer.parseInt(match.group(2));
    System.out.println("test"+testNo+": " + num);
}

Which prints

test1: 2
test2: 8
test3: 3

Note: In this example parsing the strings is only done for demonstration purposes, but it could be useful, if you want to do something with the numbers, like storing them in a array.

Update: If you also want to match strings like "ytrt.ytrwyt.test1.ytrwyt,0,2,0" you could change pattern to "test([123])\\D*,\\d+,(\\d+)," to allow any number of non-digits to follow test1, test2 or test3 (preceding the comma seperated ints).

Upvotes: 0

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726849

You can expand your pattern to include test[123] in the lookbehind, which would match test1, test2, or test3:

String pattern = "(?<=test[123][^,]{0,100},[^,]{1,100},)\\d+";
Pattern pr = Pattern.compile(pattern);
Matcher match = pr.matcher(test);
System.out.println();
while (match.find()) {
    System.out.println("Found: " + match.group());
}

The ,[^,] portion skis everything between two commas that follow testN.

I use {0,100} in place of * and {1,100} in place of + inside lookbehind expressions, because Java regex engine requires that lookbehinds had a pre-defined limit on their length. If you need to allow skipping more than 100 characters, adjust the maximum length accordingly.

Demo.

Upvotes: 1

Mena
Mena

Reputation: 48434

You can use the following Pattern and loop for this:

String test= "ytrt.ytrwyt.ytreytre.test1,0,2,0"
        + System.getProperty("line.separator")
        +"sfgtr.ytyer.qdfre.uyeyrt.test2,0,8,0"
        + System.getProperty("line.separator")
        +"sfgtr.ytyer.qdfre.uyeyrt.test3,0,3,0";
//                          | "test" literal
//                          |    | any number of digits
//                          |    |  | comma
//                          |    |  | any number of digits
//                          |    |  |    | comma
//                          |    |  |    | | group1, your digits
Pattern p = Pattern.compile("test\\d+,\\d+,(\\d+)");
Matcher m = p.matcher(test);
while (m.find()) {
    // prints back-reference to group 1
    System.out.printf("Found: %s%n", m.group(1));
}

Output

Found: 2
Found: 8
Found: 3

Upvotes: 0

Related Questions