1

I have a problem in applying a regex in my Java code.
My text string is like this (String myString)

        name: Abc Def;
        blah: 1 2 3;
        second name: Ghi;

I need to extract the name information (Abc Def). Name can contain a string of 1+ words. All the properties (name, blah, second name) are spaced with some whitespaces at the beginning

The code I'm trying to use is

String patternStr = "\\s{2,}name:\\s([\\w ]*\\w+);";
Matcher matcher = Pattern.compile(patternStr).matcher(myString);
if (matcher.matches()) 
    System.out.println(matcher.group(1));

My regex seems working fine with online tools (regex: \s{2,}name:\s([\w ]*\w+);) but when I port it to java code it simply doesn't work. Any idea of what I'm missing?

Thanks a lot


Edit: If I use matcher.find() it works fine.

2 Answers 2

5

Here ya go:

^\s*name:\s*([^;]+);
  • Start at the beginning of the line.
  • Eat as much whitespace as there is.
  • Find "name:"
  • Eat as much whitespace as there is.
  • Capture anything before the next semicolon
  • Make sure there's a semicolon
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the regex improvement but I think that the problem is when I call matcher.matches(). If I use matcher.find() it works fine.
2

The problem is probably that .match( ) tries to match the whole input line. So either you can add a .+ at the end of the pattern to eat the rest of the line or use .contains( ). (with .match( ) the ^ and $ at the start and end of the pattern are implicit)

Note that you need to provide a flag when you need to match against multi-line input: .compile(patternStr, Perl5Compiler.MULTILINE_MASK)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.