简体   繁体   中英

Regex - finding a string that occurs in another string every n (fixed) characters

My question title is probably unclear so i'm going to try explain with a few examples to better explain

I have only just started using regex, and would like to use it to match a string that occurs (hidden) within another string every n characters, for example:

We want to find the world "hello" in this string:

"h..e..l..l..o......"

Note how it repeats every 2 characters

I was able to create a simple regex rule for this as follows:

"h.{2}e.{2}l.{2}l.{2}o"

However this only works once every 2 steps, I wanted it any amount of steps (or at least say up to 5) - such that it would also match a string like:

"h...e...l...l...o"

What I did try:

"h.{1,5}e.{1,5}l.{1,5}l.{1,5}o"

However this does not fix a number in all the ranges so say this string would pass even though it doesn't appear in fixed intervals:

"h..e..l.l..o"

Notice the single character between the 2 l's. My only other thought here would be using a loop to say iterate 1 to 5 and try all of these as individual regex statements, but thought would be useful to try asking here to see if there are any neat ways I can do this with regex :)

Thankyou for any responses

I suggest using a capture group and back-reference for this:

h(\.{1,5})e\1l\1l\1o

RegEx Demo

Here (\\.{1,5}) after first letter h will match 1 to 5 dots and capture in group #1. Afterwards we use back-reference #1 ie \\1 between each character to make sure we match same number of dots between all letters.

试试''.join(re.findall(r"[a-zA-Z]",text))如果你想从文本中只提取字母,那就是一个单词。

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