ishwor kafley
ishwor kafley

Reputation: 938

regex matching the pattern

Is there a way to write a regular expression in python that matches the string of the following format:

feat(fix-validation): some texts (Aerogear-G1010)

or

feat$($fix-validation$)$:$some texts$($Aerogear-G1010$)

here, $ = represents Zero or more white space can be present

breakdown:

feat : string from a fixed subset of strings ['feat','fix','docs','breaking']

fix-validation : string of max n length

some texts : string of max m length

Aerogear-G1010 : Prefix should always be a string Aerogear- and after that some alphanumeric characters of max q length

Note : we can't escape the special characters like ( ) : - and should be in the exact same format as shown in the example below:

  1. feat(feat-new): new feature for group creation (Aerogear-1234)
  2. docs(new docs): add new document on the file repo (Aerogear-G1235)
  3. fix(fix-warnings): fix user raised concerns (Aerogear-P1230)

I was only able to match the string with fix subset of strings using pattern: '^fix|docs|feat\s+\(' . I am not able to add zero or more spaces after a matched string followed by (some texts) :

Can this be achieved ? thank you in advance :)

Upvotes: 0

Views: 53

Answers (1)

Alex
Alex

Reputation: 7045

From your regex, '^fix|docs|feat\s+\(', the \s+ matches any whitespace character between one and unlimited times. This requires at least one whitespace character.

Instead:

^(feat|docs|fix|breaking) *\( *(.*?) *\) *: *(.*?) *\( *(.*?) *\)$

I have used typed spaces ( ) but if you want to include any space character you should use \s which is equivalent to [\r\n\t\f\v ].


Should do what you are looking for, you can see what each part is doing here: https://regex101.com/r/EsF8FF/4

I'd recommend reading the explanation there and reading the python docs for the re module: https://docs.python.org/3/library/re.html

Upvotes: 1

Related Questions