Home > Net >  regex selecting specific words
regex selecting specific words

Time:03-19

i have long text and i need a regex that choose specific words without choosing another in this example:

۱. [اخلاق]ذلت

۲. [اخلاق، تصوف، مذهب]مذلت

i want to choose words:ذلت and مذلت without the words between [] and numbers.

by the way text is in persian language

CodePudding user response:

The regex to match either word would be:

/مذلت|ذلت/

The | is regex 'or'.

You can see it working here:

https://regex101.com/r/pKOHeb/1

CodePudding user response:

After clarification from your side, I came up with this:

(?<=\])(.|\s) (?=\[)

Explanation:

  • (?<=\]) is a lookbehind for ]
  • (?=\[) is a lookahead for [
  • (.|\s) matches any character (.) or (|) any whitespace (\s) between 1 and infinity times ( )

Note that this won't work for any text after the last bracket.

  • Related