简体   繁体   中英

Regex match everything except a specific character

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:

  • Must start with the beginning of the line ( ^ ) or a whitespace character ( \\s )
  • Must have one and only one *
  • Can match any character
  • Must match one more *
  • Must end with end of the line ( $ ) 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:

  1. Why does the character negation lead to the 3 character limit?
  2. Is there a better way to express "any character except" than I have done here?
  3. Any thoughts on a better regex to satisfy my requirements?

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|$)

Demo


You can also use non-capturing groups to exclude the extra matches.

(?:\s|^)\*([^\*]+?)\*(?:\s|$)

Demo 2

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM