Chris Chris - 2 months ago 6
Python Question

Looking for a regular expression used on variable string

I have a string that can vary slightly:

str1 = "[X] text [Y] abc123"
str1 = "(X ) text [y ] abc123"
str1 = "(x) text (Y) abc123"
str1 = "(X ) text (Y) abc123"
str1 = "(X ) text [Y ] abc123"
str1 = "[X] text333 [y] abc123222"


so basically X and Y are static and they can either be between
()
or
[]
. My problem is, that there can be some white spaces as well. X and Y with bracket can be assumed as delimiter to capture group 1. I am currently asking myself how to parse "text" and "abc123" in the most reliable way. How would you do it? "text" and "abc123" are variable strings. I want to use them further

Answer
import re

str1 = "( X ) text [Y] abc 123 xyz"

m = re.match( r'^[\(\[][xX ]+[\]\)]\s*(\S*)\s*[\(\[][yY ]+[\]\)]\s*(.*)$', str1)

if m:
   str1text = m.group(1)
   str1moretext = m.group(2)
   print str1text
   print str1moretext
else:
   print "No match!!"