简体   繁体   中英

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

尝试这样的事情:

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

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 eg .finditer if you want to get one such substring at a time, looping over them).

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.

尝试使用^\\w+_.*_\\w+\\.\\w+$

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 _"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM