I want to find all possible substrings inside a string with the following requirement: The substring starts with N, the next letter is anything but P, and the next letter is S or T
With the test string "NNSTL"
, I would like to get as results "NNS" and "NST"
Is this possible with Regex?
The finditer function of the regex library can help us perform the task of finding the occurrences of the substring in the target string and the start function can return the resultant index of each of them.
Try the following regex:
N[^P\W\d_][ST]
The first character is N, the next character is none of (^) P, a non-letter (\W), a digit (\d) or underscore (_). The last letter is either S or T. I'm assuming the second character must be a letter.
EDIT
The above regex will only match the first instance in the string "NNSTL"
because it will then start the next potential match at position 3: "TL"
. If you truly want both results at the same time use the following:
(?=(N[^P\W\d_][ST])).
The substring will be in group 1 instead of the whole pattern match which will only be the first character.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With