Regular expression doesn't match

I am trying to find this in a regex string.

  : [J, BASIC]
  ? [CINTERMEDIATE]
  : [D,MEDIUM]

      

the first character can be either ':' or '?' that is, white space followed by square brackets within square brackets, there are two text blocks separated by comma and / or space. the comma or space may or may not be present.

Here is what I wrote to find it

regex = re.compile('[:|?\s[\w[,\s]?\w]]+')

      

but he only finds

'C]'
'E]'
'M]'

      

+3


source to share


3 answers


Your regex does not treat [

as literals .. they are accepted as special characters (character set)

You can use the following:

[:?]\s*\[\w+(\s*,\s*)?\w+\]

      

Explanation:



  • [:?]

    the first character can be either :: or '?'
  • \s*\[

    then there is a space, then square brackets
  • \w+(\s*,\s*)?\w+

    there are two text blocks in square brackets, separated by a comma and / or a space (with an extra comma and a space)
  • \]

    close parenthesis

See DEMO

Edit: If you want to capture a match, you can use:

([:?]\s*\[\w+(?:\s*,\s*)?\w+\])

      

+3


source


[:?]\s\[[^\]]+?\]

      

Regular expression visualization



Demo Debuggex

+2


source


[:?]\s+\[[^, \]]*[, ]?[^\]]*\]

      

You can try this pattern. See demo.

https://regex101.com/r/bN8dL3/8#python

+2


source







All Articles