user175259
user175259

Reputation: 4921

How can I create a regular expression in Python?

I'm trying to create regular expressions to filter certain text from a text file. What I want to filter has this format:

word_*_word.word

So for example, I would like the python code every match. Sample results would be:

program1_0.0-1_log.build
program2_0.1-3_log.build

How can I do this?

Thanks a lot for your help

Upvotes: 0

Views: 1113

Answers (5)

ghostdog74
ghostdog74

Reputation: 342383

i don't understand why you would need a regex here. If the strings you want ends with ".build", you can do this for example

s="blah blah program1_0.0-1_log.build blah blah"    
for item in s.split():
    if item.endswith(".build"):
        print item

and that's it. If you want to do further checking, then

for item in s.split():
    if item.endswith(".build"):
        s = item.split("_")
        if len(s) != 3:
           print "not enough _"

Upvotes: 0

Alex Martelli
Alex Martelli

Reputation: 881705

Looks like you want to use a pattern such as r'\w+_.*_\w+\.\w+' -- assuming that * you have does stand for "zero or more totally arbitrary characters" (if not, then the .* part in the middle needs to be changed accordingly). Once you have the right pattern (depending exactly on what you mean by that *;-), you can re.compile it to get a regular expression object, and use the .findall method of the RE object, with your overall string as an argument, to get a list of all non-overlapping substrings matching this pattern (there are also alternatives such as e.g. .finditer if you want to get one such substring at a time, looping over them).

Upvotes: 3

ThomasH
ThomasH

Reputation: 23516

Try something like this:

r'[a-zA-Z0-9]+_[^_]+_[a-zA-Z0-9]+\.[a-zA-Z0-9]+'

Upvotes: 3

sth
sth

Reputation: 229613

Python's regular expression module is called re. You need to import it and use the provided functions:

import re
if re.match(r'\w+_.*_\w+.\w+', "some_text_abc.x"):
   print "yeah."

It is useful to prefix the regular expression string with r, so that it will be interpreted literally, without special handling for escape characters. Otherwise backslashes will be treated specially by the python interpreter and backslashes that are part of the regular expression need to be escaped.

Upvotes: 1

nacmartin
nacmartin

Reputation: 2182

try with ^\w+_.*_\w+\.\w+$

Upvotes: 0

Related Questions