erm1s
erm1s

Reputation: 13

trying to find text with wild card integers inside a string in a special format in Python

I have a piece of code that opens a file and iterates each line inside the formated text file there will be a piece of text like this:

name.x00y00.whatever

I searching for the x00y00 to see if exists, and copy what is found into a string. The problem is that the numbers change, it is always two digits following the 'x' and two digits following the 'y'. But there is no way to predict what those numbers are. What is the best way to search the text with wild cards for it's existance and copy it?

I am completely new to regex so if somebody could enlightenment me I would greatly appreciate it.

Upvotes: 1

Views: 367

Answers (3)

Ashwini Chaudhary
Ashwini Chaudhary

Reputation: 251096

Something like this:

>>> import re
>>> strs= "name.x00y00.whatever"
>>> match = re.search(r'\.(x\d{2}y\d{2})\.',strs)#returns None if pattern is not found
>>> if match:               
...     print match.group(1)
...     
x00y00

Upvotes: 1

Burhan Khalid
Burhan Khalid

Reputation: 174672

If the string will always have ., then splitting on that would also work:

>>> s = 'name.x12345y34.foo.bar.zoo'
>>> s.split('.')[1]
'x12345y34'

However, this assumes the format is fixed - its always the second section. It doesn't guarantee that the result will have numbers though, but it may not be required.

Upvotes: 0

RichieHindle
RichieHindle

Reputation: 281735

You can do it like this:

import re
print re.findall(r'\.(x\d{2}y\d{2})\.', "name.x00y00.whatever")
# Prints ['x00y00']

I've assumed that name and whatever can change too, and that the x00y00 piece is always delimited by dots.

\. matches a dot, and \d{2} means "two digits". The parentheses capture the piece of the match that they surround.

I'm using findall in case there are multiple matches.

Upvotes: 2

Related Questions