Home > Software engineering >  match everything inside of * with no whitespaces
match everything inside of * with no whitespaces

Time:04-08

I want a regex to have the following behaviour:

*test and test* -> match ok
* test and test* -> match NOT ok
*test and test * -> match NOT ok
* test and test * -> match NOT ok

I was using this regex to get it working: \*[^\s](.*?)[^\s]\*
But the problem is, in group #1, it cuts the first and last letter, so instead of having a match test and test, I end up with est and tes, I need to keep both first and last letter while also having whitespace in between the words

CodePudding user response:

That is because [^\s] actually matches a char, there are 2 of them so it matches (not captures) 2 characters.

The negated character class should be inside the capture group right after the asterix, and repeat it 1 or more times to not match an empty string.

If you want to exclude matching an asterix as well [^\s*] and match everything inside the * without leading or trailing spaces:

\*([^\s*] (?:\s [^\s*] )*)\*

The pattern in parts:

  • \* Match *
  • ( Capture group 1
    • [^\s*] Match 1 times any chars other than a whitspace char or *
    • (?:\s [^\s*] )* Optionally repeat 1 whitespace and again the first character class
  • ) Close group 1
  • \* Match *

Regex demo

CodePudding user response:

You just don't have the first and last letter inside your capture group (denoted by the parenthesis).

Try this: \*([^\s].*?[^\s])\*

  • Related