Search code examples
javaregexregular-language

how to ignore a value in a text?


I have a string like this :

EQ=ENABLED,QLPUB=50,EPRE=ENABLED

how can I ignore, the value of QLPUB? Actually I want to check this string in 3000 lines but I want to ignore 50.

is there any way to ignore it, for example with java regular expression or %s or ... ?


Solution

  • If value of QLPUB is always numeric you can use the following regex:

    ^EQ=ENABLED,QLPUB=\d*,EPRE=ENABLED$
    

    Here's an example:

    String text = "EQ=ENABLED,QLPUB=502,EPRE=ENABLED";      
    String pattern = "^EQ=ENABLED,QLPUB=\\d*,EPRE=ENABLED$";
    
    Pattern compiledPattern = Pattern.compile(pattern);
    Matcher matcher = compiledPattern.matcher(text);
    if(matcher.find()) {
        System.out.println(matcher.group());
    }
    

    If the value of QLPUB is anything but a , change the regex to:

    ^EQ=ENABLED,QLPUB=[^,]*,EPRE=ENABLED$