我应该如何编写正则表达式来匹配Java中的特定单词?
我一直试图让一个特定的正则表达式工作,但我不能让它做我需要的 我想在给定的字符串中查找OFF。我希望正则表达式只匹配关闭的字符串。我希望RegEx模式在我的android应用程序上实现它 我的条件:-我应该如何编写正则表达式来匹配Java中的特定单词?,java,regex,Java,Regex,我一直试图让一个特定的正则表达式工作,但我不能让它做我需要的 我想在给定的字符串中查找OFF。我希望正则表达式只匹配关闭的字符串。我希望RegEx模式在我的android应用程序上实现它 我的条件:- \W*(off)\W* (\d|\s|%)+off some text you have 20% OFF. Avail @ this shop. - Match some text some text office address is..... - Not To Match some te
\W*(off)\W*
(\d|\s|%)+off
some text you have 20% OFF. Avail @ this shop. - Match
some text some text office address is..... - Not To Match
some text you have 20%OFF. Avail @ this shop. - Match
some text you have 20%OFF! Avail @ this shop. - Match
some text you have 20%OFF; Avail @ this shop. - Match
some text some textofficeaddress is..... - Not To Match
1) 在第一个字母O之前,它将显示任何特殊字符或数字等。。但是在O之前没有字母表
2) 在最后一个字母F之后,它将显示任何特殊字符、数字、点或!等但是F之后没有字母表
尝试了正则表达式模式:-
\W*(off)\W*
(\d|\s|%)+off
some text you have 20% OFF. Avail @ this shop. - Match
some text some text office address is..... - Not To Match
some text you have 20%OFF. Avail @ this shop. - Match
some text you have 20%OFF! Avail @ this shop. - Match
some text you have 20%OFF; Avail @ this shop. - Match
some text some textofficeaddress is..... - Not To Match
Java编码
public static boolean offerMatcher(String message_body) {
MessageMatcher = Pattern.compile(message, Pattern.CASE_INSENSITIVE).matcher(message_body);
return MessageMatcher.find();
}
注意:-图案在For循环中
示例:-
\W*(off)\W*
(\d|\s|%)+off
some text you have 20% OFF. Avail @ this shop. - Match
some text some text office address is..... - Not To Match
some text you have 20%OFF. Avail @ this shop. - Match
some text you have 20%OFF! Avail @ this shop. - Match
some text you have 20%OFF; Avail @ this shop. - Match
some text some textofficeaddress is..... - Not To Match
我一直在尝试使用一个在线正则表达式生成器使其正确,但我无法使其精确匹配。根据模糊的描述,这符合您的要求 在
关闭之前或之后没有字母
[^a-zA-Z]OFF[^a-zA-Z]
不过,我可能会建议打个数字+折
\d+% OFF[^a-zA-Z]
根据模糊的描述,这符合您的要求
在关闭之前或之后没有字母
[^a-zA-Z]OFF[^a-zA-Z]
不过,我可能会建议打个数字+折
\d+% OFF[^a-zA-Z]
只需在场外使用[^\\p{Alpha}]
,即不使用字母字符
String [] inputs = {
"some text you have 20% OFF. Avail @ this shop.",
"some text some text office address is.....",
"some text you have 20%OFF. Avail @ this shop." ,
"some text you have 20%OFF! Avail @ this shop." ,
"some text you have 20%OFF; Avail @ this shop.",
"some text some textofficeaddress is....."};
for (String each : inputs) {
System.out.println((each + " => " + each.matches(".*[^\\p{Alpha}]OFF[^\\p{Alpha}].*")));
}
只需在场外使用[^\\p{Alpha}]
,即不使用字母字符
String [] inputs = {
"some text you have 20% OFF. Avail @ this shop.",
"some text some text office address is.....",
"some text you have 20%OFF. Avail @ this shop." ,
"some text you have 20%OFF! Avail @ this shop." ,
"some text you have 20%OFF; Avail @ this shop.",
"some text some textofficeaddress is....."};
for (String each : inputs) {
System.out.println((each + " => " + each.matches(".*[^\\p{Alpha}]OFF[^\\p{Alpha}].*")));
}
您只需在\W
之后删除*
,因为它允许在关闭前后0次出现非单词字符
问题是,如果它位于字符串的开头或结尾,它将无法找到。如果您还希望接受这些案例,请明确添加它们:
public class Match {
public static void main(String[] args) {
Pattern pattern = Pattern.compile("(\\W|^)(off)(\\W|$)", Pattern.CASE_INSENSITIVE);
String[] strings = new String[] {
"some text you have 20% OFF. Avail @ this shop.",
"some text some text office address is",
"some text you have 20%OFF. Avail @ this shop.",
"some text you have 20%OFF! Avail @ this shop.",
"some text you have 20%OFF; Avail @ this shop.",
"some text some textofficeaddress is.....",
"OFF is OK",
"test with OFF"
};
for (String s : strings) {
System.out.println(s + " : " + pattern.matcher(s).find());
}
}
}
您只需在\W
之后删除*
,因为它允许在关闭前后0次出现非单词字符
问题是,如果它位于字符串的开头或结尾,它将无法找到。如果您还希望接受这些案例,请明确添加它们:
public class Match {
public static void main(String[] args) {
Pattern pattern = Pattern.compile("(\\W|^)(off)(\\W|$)", Pattern.CASE_INSENSITIVE);
String[] strings = new String[] {
"some text you have 20% OFF. Avail @ this shop.",
"some text some text office address is",
"some text you have 20%OFF. Avail @ this shop.",
"some text you have 20%OFF! Avail @ this shop.",
"some text you have 20%OFF; Avail @ this shop.",
"some text some textofficeaddress is.....",
"OFF is OK",
"test with OFF"
};
for (String s : strings) {
System.out.println(s + " : " + pattern.matcher(s).find());
}
}
}
您忘了提到您尝试的正则表达式可以使用string.contains(“OFF”)@板球。请稍等,我会更新的。对不起,“O之前没有字母表”--这是什么意思??每个例子都在O前面有字母。你的意思是紧接着之前吗?@Mohammadnabil Contains将是最后一个例子。你忘了提到你尝试的正则表达式可以使用string.Contains(“OFF”)@板球。请稍等,我会更新的。对不起,“O之前没有字母表”--这是什么意思??每个例子都在O前面有字母。你是说紧接着之前吗?@Mohammadnabil Contains will举了最后一个例子。为什么不使用i
标志?因为案例看起来很重要?@cricket\u 007谢谢。为什么不使用i
标志?因为案例看起来很重要?@cricket\u 007谢谢。你呢\u OFF
?不知道Java有这种语法。。。我知道\p{Alpha}
那么\u OFF
呢?我不知道Java有这样的语法。。。我知道\p{Alpha}