简体   繁体   中英

Match similar string in regex

I want to find out if is it possible to match one of two or more similar lines.

Strings to be matched:

Its a string
Its a string
Its a string

Excepted result:

Its a string

Everything I tried just select every line, because they are absolutely similar.

Is it possible to always keep one similar line unmatched?

I'm not 100% sure that this will work for you, but it does what I think you're trying to do.

import re
p = re.compile(r'(^.+$)((.|\n|r)*)^\1$', re.MULTILINE)
result = p.search(string)

repeated_line = result.groups()[0].strip()

You need to specify re.MULTILINE so that it works with capturing ^$ characters.

Here's a quick brake-down of the regex:

(^.+$)          # Matches a full line and captures it into '\1'  
((.|\n|\r)*)    # Matches any number of characters/newlines  
^\1$            # Matches the first capturing group ensuring that the second occurrence fills a line and has it's own line.

There's probably better ways to do this, but this is the first solution I thought up that specifically uses regex.

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