1

I have such a regex:

(<.*?>)

It allows me to find all the substrings starting with < and ending with >.

For instance, like the following:

<IL_A_NUM>

My regex expression helps me to find the strings as above. The expression .*? involves all characters, but I want to exclude strings with < or > inside from my search. So, if < or > appears between another < and > my search won't give me that subpart.

How should I modify my regular expression?

6
  • 1
    If you want to match the outer you just need to remove?. Commented Feb 21, 2017 at 11:44
  • My plan is, firstly finding the inner part, and replacing it with another string, and recursively proceeding until when I reach to the outer boundary. I want to replace the last outer part with another string anyway. Commented Feb 21, 2017 at 11:46
  • 1
    So just do this re.sub(r'(<)(.*)(>)', r'\1your_last_character_to_replace\3', your_string) Commented Feb 21, 2017 at 11:51
  • 1
    @yusuf Python's builtin re library doesn't support recursion/nested groupings... do you have some better examples of exactly what you're trying to achieve as this seems to be a bit of an XY Problem Commented Feb 21, 2017 at 11:51
  • Jon, I plan to do it recursively in a loop. Commented Feb 21, 2017 at 11:52

1 Answer 1

1

For matching the outer sub-string you need to drop the ? and for replacing the inner matched you can just use re.sub like following:

re.sub(r'(<)(.*)(>)', r'\1your_last_character_to_replace\3', your_string)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.