0

I am trying to create an expression that will give me an output of yes for strings, 6,7,8 and an output of no for 9 & 10. Any advice would be greatly appreciated!

#The regular expression is ' ^<[^<>]*>$ '
    print ( 'The regular expression used for 5 strings was "^<[^<>]*>$"' )
    string6 = '<an xml tag>'
    string7 =  '<an xml tag>, </closetag>'
    string8 = '<with attribute="77">'
    string9 = '<opentag><closetag>'
    string10 = '</closetag>'

    if re.search( r'^<[^<>]*,>$',string6 ) :
        print( "yes" )
    else:
        print("no")
    if re.search( r'^<[^<>]*,>$',string7 ) :
        print( "yes" )
    else:
        print("no")

    if re.search(r'^<[^<>]*>$',string8):
        print("yes")

    else:
        print("no")

    if re.search(r'^<[^<>]*>$',string9):
        print("yes")

    else:
        print("no")

    if re.search(r'^<[^<>]*>$',string10):
        print("yes")

    else:
        print("no")

This is the result: yes no yes no yes

I am trying to get: yes yes yes no no

7
  • 6
    Please do not parse XML/HTML with regexes: stackoverflow.com/questions/1732348/… Commented Oct 2, 2018 at 19:30
  • 1
    re.search(r'[xw]', testedString) Commented Oct 2, 2018 at 19:31
  • 1
    Is there a meaning to these "yes" and "no" results? Commented Oct 2, 2018 at 19:31
  • Try using beautifulsoup instead. You'll probably have much better luck! Commented Oct 2, 2018 at 19:41
  • 1
    So the regex itself can be anything? And so @Ruzihm suggestion will work for you? Or (digging deeper) is there a purpose beyond each of those regexes, other than "matching"? Commented Oct 2, 2018 at 20:31

2 Answers 2

1

If you just have to match those specific strings, just use a regular expression with literal text in an alternation.

regex = r'^(?:<an xml tag>|<an xml tag>, </closetag>|<with attribute="77">)$';
for s in [string6, string7, string8, string9, string10]:
    if (re.search(regex, s)):
        print("yes")
    else:
        print("no")

It's not clear from the question what more general pattern could be used.

Sign up to request clarification or add additional context in comments.

1 Comment

Unless OP expands the question, we might as well boil it down to print ("yes yes yes no no") 😀
1

The first 3 strings have at least one space character in them and the last 2 don't. If you use a regular expression that's just a single space character, , it will match the first three and not the last two.

string6 = '<an xml tag>'
string7 =  '<an xml tag>, </closetag>'
string8 = '<with attribute="77">'
string9 = '<opentag><closetag>'
string10 = '</closetag>'

if re.search( r' ',string6 ) :
    print( "yes" )
else:
    print("no")
if re.search( r' ',string7 ) :
    print( "yes" )
else:
    print("no")

if re.search(r' ',string8):
    print("yes")

else:
    print("no")

if re.search(r' ',string9):
    print("yes")

else:
    print("no")

if re.search(r' ',string10):
    print("yes")

else:
    print("no")

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.