Skip to content
Advertisement

Where is such a regex wrong?

I am using python.
The pattern is:

JavaScript

The text like:

JavaScript

I expect to get TVTP

Advertisement

Answer

Another option to match those formats is:

JavaScript

Explanation

  • ^ Start of string
  • ([^-()]+) Capture group 1, match 1+ times any character other than - ( and )
  • (?:-[^()]*)? As the - is excluded from the first part, optionally match - followed by any char other than ( and )
  • ([^()]*) Match from ( till ) without matching any parenthesis between them

Regex demo | Python demo

Example

JavaScript

Output

JavaScript
Advertisement