简体   繁体   中英

match a string at the beginning of the line in Python

I want to search for certain pattern at the beginning of every line inside a text file.

Here is the contents of text file:

module abc ( A, B, C, NSUP, PSUP, SEL );
input NSUP;
input PSUP;
input SEL;
inout A;
inout B;
output C;
//sample text input pins
//sample text output pins

I want output as

NSUP
PSUP
SEL
A
B
C

I tried below code but it prints an empty list as output:

fh=open("VamsModel","r")
contents=fh.read()
inoutPortList=re.compile(r'^(input|output|inout)\s+(\w+)')
matches = inoutPortList.finditer(contents)

for match in matches:
    print(match.group(2))

If I remove "^" from re.compile pattern then it works but then it wont look for patterns only at the begining.

inoutPortList=re.compile(r'(input|output|inout)\s+(\w+)')

above regex will also output last two lines(shown below) from my text file which I don't want:

//sample text input pins
//sample text output pins

Any ideas why my regex not working when I am using "^" ??

With ^ on a whole buffer, you're looking for your expression at the start of the buffer.

To look for the expression at the start of each line use multiline flag:

inoutPortList=re.compile(r'^(input|output|inout)\s+(\w+)',flags=re.M)

output:

NSUP
PSUP
SEL
A
B
C

Aside: with regex module, always pass the flags as keyword parameters: flags=re.M not just re.M . It works with re.compile but not with re.sub because "count" parameter comes first, which creates weird issues.

您需要使用re.MULTILINE(re.M速记)标志来指示^匹配行首而不是字符串:

inoutPortList=re.compile(r'^(input|output|inout)\s+(\w+)', re.M)

If you know what strings you're looking for you could use startswith in place of regular expressions

if line.startswith(("input", "output", "inout")):
    print(line.split(" ", 1)[1])

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