我想捕获任何与%[a-z0-9]匹配的字符串,遵循以下示例:
1. %xxxxxxxxxxxxx //match
2. this will work %xxxxxx but not this%xxxxxxxxx. //match 1st, not 2nd
3. and also %xxxxxxxxxx. //match
4. just a line ending with %xxxxxxxxxxx //match
5. %Xxxxxxxxxxx //no match
6. 100% of dogs //no match
7. 65%. Begining of new phrase //no match
8. 65%.Begining of new phrase //no match
它可以在字符串的开头或结尾,但不能在单词的中间。当然,它可以作为单词在字符串中(用空格分隔)。
我试过了
/('b)%[a-z0-9]+('b)/
/(^|'b)%[a-z0-9]+($|'b)/
/('w)%[a-z0-9]+('w)/
和其他类似的人,但我无法让它像我一样工作。我猜 ''b 标记在示例 2 中不起作用,因为在 % 符号之前有一个边界。
任何帮助将不胜感激。
尝试
/'B%[a-z0-9]+'b/
在空格和%
之间没有词边界'b
,但在s
和%
之间有一个。
'B
与'b
不是单词边界相反。
在正则表达式101上看到它
%[a-z0-9]+(?='s|$)|(?:^|(?<='s))%[a-z0-9]+
试试这个。请参阅演示。
https://regex101.com/r/iS6jF6/20
$re = "/%[a-z0-9]+(?=''s|$)|(?:^|(?<=''s))%[a-z0-9]+/m";
$str = "1. %xxxxxxxxxxxxx //match'n2. this will work %xxxxxx but not this%xxxxxxxxx. //match 1st, not 2nd'n3. and also %xxxxxxxxxx. //match'n4. just a line ending with %xxxxxxxxxxx //match'n5. %Xxxxxxxxxxx //no match'n6. 100% of dogs //no match'n7. 65%. Begining of new phrase //no match'n8. 65%.Begining of new phrase //no match";
preg_match_all($re, $str, $matches);
或
%[a-z0-9]+'b|'b%[a-z0-9]+