Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding all possible substrings within a string. Python Regex

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?

like image 334
Rodrigo Villalba Zayas Avatar asked Feb 26 '14 02:02

Rodrigo Villalba Zayas


People also ask

How do you find all occurrences of a substring in a string in Python?

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.


1 Answers

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.

like image 105
CJ Dennis Avatar answered Sep 26 '22 17:09

CJ Dennis