3

I want to detect some special sub string and also overlapping.I have a string from input and if the string comprises 'AB' and 'BA'(both of them) I print in out 'yes' and if only comprises 'ABA' or 'BAB'(overlap) the output is 'NO'. I wrote the following code but I receive error. the problem is in re.search() in if . how can I use correctly re.search for this code? Thanks in advance for your help

import re
str1=input()
if re.search('AB',str1):
        if re.search('BA',str1):
            if re.search('ABA'|'BAB',str1):
                if re.search('ABBA'|'BAAB',str1):
                    print('YES')
                print('NO')
            print('YES')
        else :
           print('NO')
else:
      print('NO')
2
  • Can you provide some examples of input that should print yes and some that shouldn't. It's difficult to understand exactly what you want. Commented Nov 27, 2018 at 12:01
  • for example if the input comprised 'ABBA' or 'BAAB' or 'AABDABBA' we print 'YES' but if it comprised only 'AB' or 'ABCA' or 'BA' or 'BAA' or 'ABA' or 'BAB' we print 'NO' . the 'ABA' and 'BAB' called the overlap of AB and BA for first one (according to reading the string) and for the second one( 'BAB' ) is BA and AB. Commented Nov 27, 2018 at 14:48

1 Answer 1

3

You could directly check for the pattern instead of worrying about overlap (as this is what regex is good for).

(I have made an assumption here that a string ABAxyzBAB should print 'YES' since it contains cases of AB and BA in individual cases and not just an overlap)

import re
str1=input()
if re.search(r'AB.*?BA', str1):
    print('YES')
elif re.search(r'BA.*?AB', str1):
    print('YES')
else:
    print('NO')

What this does is, it first checks to see if a part of the string matches AB, it then looks after the AB to find a BA, if this happens it prints out 'YES'. Otherwise it tries to do the opposite, it will then check to see if part of the string matches BA, then it will look after the BA to find an AB. If it finds an AB afterwards it prints out 'YES'. In the case that neither of these happen it prints out 'NO'

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

5 Comments

it's very good .thank you .can you tell me why do I get error?
@hasa it's probably because of how you setup your or statements, they should be in one regex string not separate strings so it should be r'(ABA)|(BAB)' instead of just 'ABA'|'BAB'
yes you're right. I tried it and error vanished!. I think it's not necessary to put r in the beginning because I removed it and code ran without any error.
@hasa The r at the start defines the string as a raw string and makes it so that you don't need to escape backslashes which sometime become tedious to always escape in regex pattern. (We are lucky in this case that we have no backslashes in our pattern so it makes no difference)
thank you, your explanation is conceptual and accurate.

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.