开发者

regex starts with '_' and ends with white space/new line in Java

I want to be able to locate this pattern '_*' meaning starts with '_' and ends with either a wh开发者_如何转开发ite space or a new line.

also I have another regex to locate a word of capital letters only: [A-Z]+ is this accurate, or there's a better way of writing it?

I use Java.


If you try

String _command = "AFTER_2011/03/01 GREATER_2004";
Pattern patt = Pattern.compile("_\\S+");
Matcher matcher = patt.matcher(_command);
while(matcher.find()) {
    String name = _command.substring(matcher.start()+1, matcher.end());
    System.out.println(name);
}

it prints

2011/03/01
2004

EDIT: As @Alan Moore suggests you can do the following.

String _command = "AFTER_2011/03/01 GREATER_2004";
Pattern patt = Pattern.compile("_(\\S+)");
Matcher matcher = patt.matcher(_command);
while(matcher.find()) {
    String name = matcher.group(1);
    System.out.println(name);
}


As far as [A-Z]+ goes, it will work with latin input. So will \p{Upper}+

If you need to support a wider character set, consider \p{Lu}+ which will match any upper case unicode character.

Pattern's javadoc is good! http://download.oracle.com/javase/6/docs/api/java/util/regex/Pattern.html


Pattern pattern = Pattern.compile("_.*(\\s|\\n)");
Matcher matcher = pattern.matcher(_command);

// Find all matches
while (matcher.find()) {
  // Get the matching string
  String match = matcher.group();
}    

[A-Z]+ is fine

If you want code too let me know


If regex is _.*(\s|\n) using Java, you'll have to write the same as _.*(\\s|\\n)

Update:

Resulting groups are those parts of the regex within brackets. So currently get only the last space or line break.

Try with this regex:

(_.*\\s+)

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜