Philipp2706
Philipp2706

Reputation: 39

My regex does not work in Java and returns null

I want to extract the values on the right side in each row. Unfortunately my regex in Java does not work and group("val") returns null. In an online regex-syntaxchecker for PHP everything works fine.

Can anyone help me or have suggestions for improvement?

Thanks in advance

*************************************************************************
|                              Statistics                               | 
|                                                                       |
|  Tags                        : 2123123#1,dasd#1,gfgfg#1,sdfsfd#1,ww#1 |
|  Notes count (user)          : 5                                      |
|  Notes count (total)         : 5                                      |
|  User rank                   : Rookie                                 |
|  Average length (user)       : 4.8                                    |
|  Average length (total)      : 4.8                                    |
|  Average words (user)        : 0.8                                    |
|  Average words (total)       : 0.8                                    |
|  Most used category (user)   : IMPORTANT                              |
|  Most used category (total)  : IMPORTANT                              |
|  Least used category (user)  : IMPORTANT                              |
|  Least used category (total) : IMPORTANT                              |
|  Most used color             : 165#42#42#255                          |
|  Least used color            : 165#42#42#255                          |
|  Top Poster                  : admin                                  |
|  Notes today                 : 0                                      |
|  Categorie Occasion          : IMPORTANT#5                            |
|  Number of Stats             : 17                                     |
|                                                                       |
|  Statistics terminated with no errors                                 |
*************************************************************************




public static String extractString(String s, String tag) {

    final String regex = "|\\s{2}Tags\\s*:\\s(?<val>[^|]*)\\s|$";       
    final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
    Matcher m = pattern.matcher(s);
     
    String value = "";
    if (m.find()) {
         value = m.group("val");
     }
    
    return value;
  }

Upvotes: 0

Views: 85

Answers (1)

pcsutar
pcsutar

Reputation: 1821

Try with regex ^\|\s{2}Tags\s*:\s(?<val>.*)\s+\|$ it will work

Check regex at https://regex101.com/r/W5VR9D/1/

Java code:

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

public class Solution {

    public static void main(String[] args) {
        String data = "*************************************************************************\r\n" +
                "|                              Statistics                               | \r\n" +
                "|                                                                       |\r\n" +
                "|  Tags                        : 2123123#1,dasd#1,gfgfg#1,sdfsfd#1,ww#1 |\r\n" +
                "|  Notes count (user)          : 5                                      |\r\n" +
                "|  Notes count (total)         : 5                                      |\r\n" +
                "|  User rank                   : Rookie                                 |\r\n" +
                "|  Average length (user)       : 4.8                                    |\r\n" +
                "|  Average length (total)      : 4.8                                    |\r\n" +
                "|  Average words (user)        : 0.8                                    |\r\n" +
                "|  Average words (total)       : 0.8                                    |\r\n" +
                "|  Most used category (user)   : IMPORTANT                              |\r\n" +
                "|  Most used category (total)  : IMPORTANT                              |\r\n" +
                "|  Least used category (user)  : IMPORTANT                              |\r\n" +
                "|  Least used category (total) : IMPORTANT                              |\r\n" +
                "|  Most used color             : 165#42#42#255                          |\r\n" +
                "|  Least used color            : 165#42#42#255                          |\r\n" +
                "|  Top Poster                  : admin                                  |\r\n" +
                "|  Notes today                 : 0                                      |\r\n" +
                "|  Categorie Occasion          : IMPORTANT#5                            |\r\n" +
                "|  Number of Stats             : 17                                     |\r\n" +
                "|                                                                       |\r\n" +
                "|  Statistics terminated with no errors                                 |\r\n" +
                "*************************************************************************";

        System.out.println(extractString(data));
    }

    public static String extractString(String s) {

        final String regex = "^\\|\\s{2}Tags\\s*:\\s(?<val>.*)\\s+\\|$";
        final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
        Matcher m = pattern.matcher(s);

        String value = "";
        if (m.find()) {
            value = m.group("val");
        }

        return value;
    }
}

Upvotes: 2

Related Questions