-3

i am trying to extract the phone but the expression returns none. here is my code.it must start with 9 and end with number with only number inbetween.

line='my phone is 98849448470'
matchnum=re.search(r"^9[0-9]*[0-9]$",line)
print(matchnum)
Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153

1 Answers1

0
matchnum=re.search(r"^9[0-9]*[0-9]$",line)

The ^ at the start and $ at the end force the expression to try to match the entire line. However, your actual line contains other stuff besides what you want to search for. So leave those out.

Also, instead of [0-9]*[0-9] for one or more matches, we can simply use [0-9]+.

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
  • hi, i thought it would search inside the entire line and returns the value after finding it.i did't knew it checks the entire line.thanks anyway.i am newbie here making some mistake – Sahil Xresta Apr 24 '20 at 12:35