user765443
user765443

Reputation: 1892

Added some regex into existing regular pattern

I am not good regex and need to update following pattern without impacting other pattern. Any suggestion $ sign contain 1t0 4. $ sign always be begining of the line.( space may or may not be)

import re
data = " $$$AKL_M0_90_2K: Two line end vias (VIAG, VIAT and/or" 
patt = '^ (?:ABC *)?([A-Za-z0-9/\._\:]+)\s*: ? '
match = re.findall( patt, data, re.M )
print match

Note : data is multi line string

match should contain : "$$$AKL_M0_90_2K" this result

Upvotes: 1

Views: 69

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626845

I suggest the following solution (see IDEONE demo):

import re
data = r" $$$AKL_M0_90_2K: Two line end vias (VIAG, VIAT and/or" 
patt = r'^\s*([$]{1,4}[^:]+)'
match = re.findall( patt, data, re.M )
print(match)

The re.findall will return the list with just one match. The ^\s*([$]{1,4}[^:]+) regex matches:

  • ^ - start of a line (you use re.M)
  • \s* - zero or more whitespaces
  • ([$]{1,4}[^:]+) - Group 1 capturing 1 to 4 $ symbols, and then one or more characters other than :.

See the regex demo

If you need to keep your own regex, just do one of the following:

  • Add $ to the character class (demo): ^ (?:ABC *)?([$A-Za-z0-9/._:]+)\s*: ?
  • Add an alternative to the first non-capturing group and place it at the start of the capturing one (demo): ^ ((?:ABC *|[$]{1,4})?[A-Za-z0-9/._:]+)\s*: ?

Upvotes: 1

Related Questions