Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Regex to check the string pattern

From the following string have to check the presence of pattern [TEST-SELOGER][0][WARN][Tune][5121]

2021 Nov 22 07:07:03.373538 Process[15186]: [TEST-SELOGER][0][WARN][Tune][5121]GROUND TEST[0] test_tune: try: 1 format:

Here
[TEST-SELOGER]- will not change

[0]- integer values only , negative values not supported

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

[WARN]-Value will not change

[Tune]- Value will not change

[5121]-Dynamic integer values, negative values not supported

Which regex I can use to check the presence of pattern [TEST-SELOGER][0][WARN][Tune][5121]

>Solution :

You can use the below regex:

\[TEST-SELOGER\]\[([0-9]+)\]\[WARN\]\[Tune\]\[([0-9]+)\]

But, note that in Java, you need to use \\ instead of \.

Code:

import java.util.regex.*;

public class Main
{
    public static void main(String[] args) {
        String test = "2021 Nov 22 07:07:03.373538 Process[15186]: [TEST-SELOGER][0][WARN][Tune][5121]GROUND TEST[0] test_tune: try: 1 format:";
        Pattern pattern = Pattern.compile("\\[TEST-SELOGER\\]\\[([0-9]+)\\]\\[WARN\\]\\[Tune\\]\\[([0-9]+)\\]");
        Matcher matcher = pattern.matcher(test); 
        if (matcher.find())
        {
            System.out.println(matcher.group(1) + " " + matcher.group(2));
        }
        else
        {
            System.out.println("No match");
        }
        
    }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading