Home > other >  How to find all occurrences of a specific word after a symbol?
How to find all occurrences of a specific word after a symbol?

Time:12-08

For example, finding all xxx after the symbol :

"bla bla bla xxx bla bla": "bla bla xxx bla bla bla xxx bla bla xxx",

"bla bla bla bla bla": "bla bla xxx",

"xxx": "xxx",

"some nice text about xxx": "but i want this xxx and this xxx",

I need a regex to use in vscode search tool and replace xxx in multiple files.

Thanks.

CodePudding user response:

try this :

((?<=:.*)xxx)*

this part (?<=:.*) is a positive lookbehind. To match, a string need to match :.* before the actual match. :.* mean match a : and any character afterward.

it match only what you want :

"bla bla bla xxx bla bla": "bla bla xxx bla bla bla xxx bla bla xxx"

"bla bla bla xxx bla bla": "bla bla xxx bla bla bla xxx bla bla xxx"

Verify it here : regexr.com/6b4rs

CodePudding user response:

well, you can split the string and than look in the second part like this:

str1 = "bla bla bla xxx bla bla: bla bla xxx bla bla bla xxx bla bla xxx"
str2 = str1.split(":")
str2.pop(0)
str2 = "".join(str2)
result = re.findall("(xxx)", str2)

that will look for all "xxx" after the first ":"

  • Related