正则表达式匹配冒号后面的字符串“:”

时间:2021-03-18 11:08:34

I need to identify a sub string from any string based on a regular expression.

我需要根据正则表达式从任何字符串中识别子字符串。

For Example, take the following strings:

例如,请使用以下字符串:

  1. It sent a notice of delivery of goods: UserName1
  2. 它发送了货物交付通知:UserName1
  3. Sent a notice of delivery of the goods: User is not found, UserName2
  4. 发送货物交货通知:未找到用户,UserName2
  5. It sent a notice of receipt of the goods: UserName1
  6. 它发送了收货通知:UserName1
  7. It sent a notice of receipt of the goods: User is not found, UserName2
  8. 它发送了收货通知:未找到用户,UserName2

I want to get the text after colon

我希望在冒号之后得到文本

  1. UserName1
  2. USERNAME1
  3. User is not found, UserName2
  4. 找不到用户,UserName2
  5. UserName1
  6. USERNAME1
  7. User is not found, UserName2
  8. 找不到用户,UserName2

2 个解决方案

#1


1  

You can use the regex like this:

您可以像这样使用正则表达式:

":(.*)"

Then, you should use something like this one (on Java):

然后,您应该使用类似这样的东西(在Java上):

Matcher m = Pattern.compile(":(.*)").matcher(text);

if (m.find())
{
    System.out.println(m.group(1));
}

#2


0  

import java.util.regex.Matcher; import java.util.regex.Pattern;

public class MatchColon {

    private Pattern pattern;
    private Matcher matcher;

    private static final String MATCHCOLON_PATTERN =
        ":(.*)";

    public MATCHCOLON(){
      pattern = Pattern.compile(MATCHCOLON_PATTERN);
    }

    public boolean validate(final String colon){
      matcher = pattern.matcher(colon);
      return matcher.matches();
    }
}

#1


1  

You can use the regex like this:

您可以像这样使用正则表达式:

":(.*)"

Then, you should use something like this one (on Java):

然后,您应该使用类似这样的东西(在Java上):

Matcher m = Pattern.compile(":(.*)").matcher(text);

if (m.find())
{
    System.out.println(m.group(1));
}

#2


0  

import java.util.regex.Matcher; import java.util.regex.Pattern;

public class MatchColon {

    private Pattern pattern;
    private Matcher matcher;

    private static final String MATCHCOLON_PATTERN =
        ":(.*)";

    public MATCHCOLON(){
      pattern = Pattern.compile(MATCHCOLON_PATTERN);
    }

    public boolean validate(final String colon){
      matcher = pattern.matcher(colon);
      return matcher.matches();
    }
}