开发者

Regex - extract info from a String in Java

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.


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


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)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜