简体   繁体   English

使用正则表达式忽略Java中的模式

[英]Using Regex to ignore a pattern in java

I have a sentence: "we:PR show:V" . 我有一句话: "we:PR show:V" I want to match only those characters after ":" and before "\\\\s" using regex pattern matcher. 我想使用正则表达式模式匹配器仅匹配":""\\\\s"之前的那些字符。 I used following pattern: 我使用以下模式:

Pattern pattern=Pattern.compile("^(?!.*[\\w\\d\\:]).*$");

But it did not work. 但这没有用。 What is the best pattern to get the output? 获得输出的最佳模式是什么?

For a situation such as this, if you are using java, it may be easier to do something with substrings: 对于这种情况,如果您使用的是Java,则使用子字符串执行操作可能会更容易:

String input = "we:PR show:V";
String colon = ":";
String space = " ";
List<String> results = new ArrayList<String>();
int spaceLocation = -1;
int colonLocation = input.indexOf(colon);
while (colonLocation != -1) {
    spaceLocation = input.indexOf(space);
    spaceLocation = (spaceLocation == -1 ? input.size() : spaceLocation);
    results.add(input.substring(colonLocation+1,spaceLocation);

    if(spaceLocation != input.size()) {
        input = input.substring(spaceLocation+1, input.size());
    } else {
        input = new String(); //reached the end of the string
    }
}
return results;

This will be faster than trying to match on regex. 这比尝试在正则表达式上匹配要快。

The following regex assumes that any non-whitespace characters following a colon (in turn preceded by non-colon characters) are a valid match: 以下正则表达式假定冒号后面的任何非空白字符(依次是非冒号字符)都是有效的匹配项:

[^:]+:(\S+)(?:\s+|$)

Use like: 使用方式如下:

String input = "we:PR show:V";
Pattern pattern = Pattern.compile("[^:]+:(\\S+)(?:\\s+|$)");
Matcher matcher = pattern.matcher(input);
int start = 0;
while (matcher.find(start)) {
    String match = matcher.group(1); // = "PR" then "V"
    // Do stuff with match
    start = matcher.end( );
}

The pattern matches, in order: 模式匹配,顺序为:

  1. At least one character that isn't a colon. 至少一个不是冒号的字符。
  2. A colon. 冒号。
  3. At least non-whitespace character (our match). 至少是非空白字符(我们的匹配项)。
  4. At least one whitespace character, or the end of input. 至少一个空格字符或输入结尾。

The loop continues as long as the regex matches an item in the string, beginning at the index start , which is always adjusted to point to after the end of the current match. 只要正则表达式与字符串中的某项匹配(从索引start ,循环就会继续,该索引始终会调整为指向当前匹配结束之后的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM