Reputation: 5512
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.
Upvotes: 1
Views: 960
Reputation: 23373
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)
Upvotes: 2
Reputation: 882
Here ya go:
^\s*name:\s*([^;]+);
Upvotes: 5