aceminer
aceminer

Reputation: 4295

Python Regex match and search doesnt work

Suppose i have this regex.

[0-9 ]{5,7}

I am using python.

import re
re.search(r'[0-9 ]{5,7}', '1123124213')

This will return a positive for this.

However, if i were to use

re.match(r'[0-9 ]{5,7}', '1123124213')

However, my string is usually something like:

I am going home tonight call me at 21314123 

Tokenization does not work either. How do i resolve this?

I would like to match this.

I am going home tonight call me at 21314

but not

I am going home tonight call me at 21314123 

However, if i were to use regex i would be able to search both 21314 and 21314123

Basically i have a list of strings containing such strings.

I am going home tonight call me at 213123 
I am going home tonight call me at 21313 
I am going home tonight call me at 2131 
I am going home tonight call me at 21314213123 
I am going home tonight call me at 21314 

and i would like to use regex/ any methods to extract those that contain

I am going home tonight call me at 21314

Upvotes: 0

Views: 55

Answers (1)

paidhima
paidhima

Reputation: 2392

re.search will find your pattern anywhere in the string, while re.match will only find it at the beginning. So, for your example string, re.match won't return a value.

Upvotes: 2

Related Questions