I am trying to set up a regex that will match the following:
*test*
*t*
*te*
But, I do not want it to match:
*test**
The general rules are:
^
) or a whitespace character ( \\s
) *
*
$
) or a whitespace character ( \\s
) I have generated the following regex:
(\s|^)\*([^\*].+?[^\*])\*(\s|$)
This nearly satisfies my requirements; however, because of the two [^\\*]
groups within the second capturing group, it seems to require that capturing group to be 3 characters or more. *tes*
matches, but *t*
and *te*
do not.
I have three specific questions:
The problem in the regex is an extra .
in the capturing group
[^\*].+?[^\*]
^
This will match a character except *
followed by one or more of any characters except newline.
As the character class is repeated twice, you can use +
quantifier to match one or more characters.
(\s|^)\*([^\*]+?)\*(\s|$)
You can also use non-capturing groups to exclude the extra matches.
(?:\s|^)\*([^\*]+?)\*(?:\s|$)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.