我正在寻找一个完成以下任务的正则表达式:
**Only single**, alphanumeric p**art**s of **words** enclosed by two asterisks should be matched.****
从这个字符串中,“art”和“words”应该被过滤掉。
到目前为止,我已经得到了\*{2}(\w+)
,但我一直在试图弄清楚如何处理结束的星号。
你可以试试这个\*{2}([A-Za-z0-9]+)\*{2}
\* <- An asterisk (escaped as * is already a symbol)
{2} <- Repeated twice
( <- Start of a capturing group
[A-Za-z0-9] <- An alphanum (careful with \w, it's the equivalent of [a-zA-Z0-9_] not [a-zA-Z0-9])
+ <- Repeated at least once
) <- End of the capturing group
\* <- An asterisk (again)
{2} <- Repeated twice
这个怎么样?
preg_match_all('~(?<=\*\*)[a-z\d]+(?=\*\*)~i', $string, $matches);
在这里查看它的实际操作:http ://codepad.viper-7.com/OJHzEs
这里有一个详细的解释: