Home > Enterprise >  python regular expression for "_n1_n2_n1_n3_n1_n1_n2"
python regular expression for "_n1_n2_n1_n3_n1_n1_n2"

Time:03-10

Lets say I have the following string,

My ID is _n1_n2_n1_n3_n1_n1_n2 ,

I'm looking to extract the _n1_n2_n1_n3_n1_n1_n2, we only need to consider word where _n occurs between 5-10 times in a word. the numbers followed by _n anywhere between 0-9.

import re
str = 'My ID is _n1_n2_n1_n3_n1_n1_n2'
match = re.search(r'_n\d{0,9}', str)
if match:
    print('found', match.group())
else:
    print('did not find')

I was able to extract the _n1 with _n\d{0,9} but unable to extend further. Can any one help me to extend further in python.

CodePudding user response:

I'm not sure if this is what you want but how about:

(_n\d) 

Explanation:

  • (..) signifies a group
  • means we want the group to repeat 1 or more times
  • _n\d means we want to have _n followed by a number

CodePudding user response:

You need a regex that sees 7 times a _n\d : '(_n\d){7}'

match = re.search(r'(_n\d){7}', value)

  • (_n\d){4,8} for range of amount
  • (_n\d) for any amount

CodePudding user response:

In Regex, {0,9} is not a number between 0 and 9, it's an amount of occurrences for the term that is in front of that, which can be a single character or a group (in parentheses).

If you want single digits from 0 to 9, that is [0-9], which is almost identical to \d (but may include non-arabic digits).

So, what you need is either

(_n[0-9]) 

or

(_n\d) 

(online), where is the number of occurrences from 1 to infinity.

From the comment

@KellyBundy I mean _n occurs 5-10 times, sorry for wrong phrasing the question.

you can further restrict to be

(_n\d){5,10}

(online)

  • Related