0

我正在寻找一个完成以下任务的正则表达式:

 **Only single**, alphanumeric p**art**s of **words** enclosed by two asterisks should be matched.****

从这个字符串中,“art”和“words”应该被过滤掉。

到目前为止,我已经得到了\*{2}(\w+),但我一直在试图弄清楚如何处理结束的星号。

4

3 回答 3

1

你可以试试这个\*{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
于 2013-01-15T18:11:43.250 回答
1

这个怎么样?

\*{2}([a-zA-Z0-9]+)\*{2}

在此处输入图像描述

于 2013-01-15T18:12:18.933 回答
0
preg_match_all('~(?<=\*\*)[a-z\d]+(?=\*\*)~i', $string, $matches);

在这里查看它的实际操作:http ://codepad.viper-7.com/OJHzEs


这里有一个详细的解释:

在此处输入图像描述

于 2013-01-15T18:12:02.080 回答