I need to identify a sub string from any string based on a regular expression.
我需要根据正则表达式从任何字符串中识别子字符串。
For Example, take the following strings:
例如,请使用以下字符串:
- It sent a notice of delivery of goods: UserName1
- 它发送了货物交付通知:UserName1
- Sent a notice of delivery of the goods: User is not found, UserName2
- 发送货物交货通知:未找到用户,UserName2
- It sent a notice of receipt of the goods: UserName1
- 它发送了收货通知:UserName1
- It sent a notice of receipt of the goods: User is not found, UserName2
- 它发送了收货通知:未找到用户,UserName2
I want to get the text after colon
我希望在冒号之后得到文本
- UserName1
- USERNAME1
- User is not found, UserName2
- 找不到用户,UserName2
- UserName1
- USERNAME1
- User is not found, UserName2
- 找不到用户,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();
}
}