我一直在努力让特定的正则表达式工作,但我无法让它做我需要的。
我想在给定字符串中查找 OFF 。我希望Regex只匹配 OFF 字符串。我希望RegEx模式在我的Android应用程序上实现它。
我的条件: -
1)在第一个字母 O 之前,无论是任何特殊字符或数字等,都可以......但 O 之前没有字母。
2)在最后一封信 F 之后,它将是任何特殊字符或数字或点或者!等等。但 F 之后没有字母。
尝试过RegEx模式: -
\W*(off)\W*
(\d|\s|%)+off
Java编码
public static boolean offerMatcher(String message_body) {
MessageMatcher = Pattern.compile(message, Pattern.CASE_INSENSITIVE).matcher(message_body);
return MessageMatcher.find();
}
注意: - 模式在For循环中。
示例: -
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
我一直试图在线使用正则表达式生成器,但我无法让它完全匹配。
答案 0 :(得分:1)
根据模糊的描述,这符合您的要求。
public function between_days($start_date, $end_date)
{
$start_date = strtotime($start_date);
$start_date = strtotime(date('d/m/Y'));
$end_date = strtotime($end_date);
$between = abs($end_date - $start_date);
$total = $between / 86400;
$days = intval($total);
return $days;
}
OFF
虽然,我可能建议使用+ +%OFF。
[^a-zA-Z]OFF[^a-zA-Z]
答案 1 :(得分:1)
只在OFF工作周围使用query.setParameter(1, name);
,即不是字母字符。
open -a Google\ Chrome --args --disable-web-security --user-data-dir=""
答案 2 :(得分:1)
您只需要在*
之后删除\W
,因为它允许在OFF之前和之后出现0个非单词字符。
问题是,如果它在字符串的开头或结尾,它将不会找到OFF。如果您还想接受这些情况,请明确添加:
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());
}
}
}