1

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

1
  • let me know if it's not clear still Commented Feb 26, 2016 at 8:35

1 Answer 1

1

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*: ?
Sign up to request clarification or add additional context in comments.

3 Comments

I was trying to add into this exiting one so other rule will not impact. is it possible. I mean add more into existing one "patt = '^ (?:ABC )? (\s)?([A-Za-z0-9/\._\:]+)\s*: ? '" like way
I added two more solutions based on your own regex. Note the . and : inside a character class do not have to be escaped. : does not have to be escaped anywhere in a Python regex.
Thx for nice explanation

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.