1

A follow up on question Python Regex - replace a string not located between two specific words as the answers were incomplete.

Given a string str, split according to "::", while disregarding "::" that are between "<" and ">" brackets.

Expected inputs and outputs:

input  a :: <<a :: b> c>::<a < a < b:: b> :: b> :: b> ::      a
output [a , <<a :: b> c>,<a < a < b:: b> :: b> :: b> ,      a]

input a< b <c a>>
output [a< b <c a>>]

input a:<a b>
output [a:<a b>]
10
  • There's an answer in the link, what's the problem? Commented Apr 19, 2015 at 8:03
  • [i for i in regex.split(r'(<(?:(?R)|[^<>])*>)|\s?::\s?', s) if i] will work. Commented Apr 19, 2015 at 8:05
  • The answer is incomplete, as a< b <c a>> (no ":" )is being split into ['a', '< b <c a>>']. I'll remove the check sign from the accepted answer. Commented Apr 19, 2015 at 8:05
  • @ErezO which input string you used? Commented Apr 19, 2015 at 8:07
  • s = "a< b <c a>>", res = [i for i in regex.split(r'(<(?:(?R)|[^<>])*>)|\s?::\s?', s) if i], res = ['a', '< b <c a>>']. Basically, the 2nd input isn't working. Commented Apr 19, 2015 at 8:09

1 Answer 1

1

Just an if else condition is needed for this case. This would do splitting if there is any :: substring present inside the input string else it would return the actual input string.

>>> def csplit(s):
        if '::' in s:
            return [i for i in regex.split(r'(<(?:(?R)|[^<>])*>)|::', s) if i and i != ' ']
        else:
            return s


>>> csplit('a :: <<a :: b> c>::<a < a < b:: b> :: b> :: b> ::      a')
['a ', '<<a :: b> c>', '<a < a < b:: b> :: b> :: b>', '      a']
>>> csplit('a:<a b>')
'a:<a b>'
>>> csplit('a< b <c a>>')
'a< b <c a>>'
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.