Pattern Recognition by re.search()
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')
regex python-3.x pattern-recognition
add a comment |
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')
regex python-3.x pattern-recognition
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.
– Borisu
Nov 27 '18 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.
– hasa
Nov 27 '18 at 14:48
add a comment |
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')
regex python-3.x pattern-recognition
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')
regex python-3.x pattern-recognition
regex python-3.x pattern-recognition
edited Nov 27 '18 at 12:11
casualcoder
887723
887723
asked Nov 27 '18 at 11:16
hasahasa
345
345
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.
– Borisu
Nov 27 '18 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.
– hasa
Nov 27 '18 at 14:48
add a comment |
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.
– Borisu
Nov 27 '18 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.
– hasa
Nov 27 '18 at 14:48
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.
– Borisu
Nov 27 '18 at 12:01
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.
– Borisu
Nov 27 '18 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.
– hasa
Nov 27 '18 at 14:48
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.
– hasa
Nov 27 '18 at 14:48
add a comment |
1 Answer
1
active
oldest
votes
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'
it's very good .thank you .can you tell me why do I get error?
– hasa
Nov 27 '18 at 13:26
1
@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 ber'(ABA)|(BAB)'
instead of just'ABA'|'BAB'
– casualcoder
Nov 27 '18 at 13:40
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
Nov 27 '18 at 14:58
1
@hasa Ther
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)
– casualcoder
Nov 27 '18 at 15:44
thank you, your explanation is conceptual and accurate.
– hasa
Nov 27 '18 at 15:54
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53498465%2fpattern-recognition-by-re-search%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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'
it's very good .thank you .can you tell me why do I get error?
– hasa
Nov 27 '18 at 13:26
1
@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 ber'(ABA)|(BAB)'
instead of just'ABA'|'BAB'
– casualcoder
Nov 27 '18 at 13:40
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
Nov 27 '18 at 14:58
1
@hasa Ther
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)
– casualcoder
Nov 27 '18 at 15:44
thank you, your explanation is conceptual and accurate.
– hasa
Nov 27 '18 at 15:54
add a comment |
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'
it's very good .thank you .can you tell me why do I get error?
– hasa
Nov 27 '18 at 13:26
1
@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 ber'(ABA)|(BAB)'
instead of just'ABA'|'BAB'
– casualcoder
Nov 27 '18 at 13:40
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
Nov 27 '18 at 14:58
1
@hasa Ther
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)
– casualcoder
Nov 27 '18 at 15:44
thank you, your explanation is conceptual and accurate.
– hasa
Nov 27 '18 at 15:54
add a comment |
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'
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'
edited Nov 27 '18 at 11:54
answered Nov 27 '18 at 11:47
casualcodercasualcoder
887723
887723
it's very good .thank you .can you tell me why do I get error?
– hasa
Nov 27 '18 at 13:26
1
@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 ber'(ABA)|(BAB)'
instead of just'ABA'|'BAB'
– casualcoder
Nov 27 '18 at 13:40
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
Nov 27 '18 at 14:58
1
@hasa Ther
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)
– casualcoder
Nov 27 '18 at 15:44
thank you, your explanation is conceptual and accurate.
– hasa
Nov 27 '18 at 15:54
add a comment |
it's very good .thank you .can you tell me why do I get error?
– hasa
Nov 27 '18 at 13:26
1
@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 ber'(ABA)|(BAB)'
instead of just'ABA'|'BAB'
– casualcoder
Nov 27 '18 at 13:40
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
Nov 27 '18 at 14:58
1
@hasa Ther
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)
– casualcoder
Nov 27 '18 at 15:44
thank you, your explanation is conceptual and accurate.
– hasa
Nov 27 '18 at 15:54
it's very good .thank you .can you tell me why do I get error?
– hasa
Nov 27 '18 at 13:26
it's very good .thank you .can you tell me why do I get error?
– hasa
Nov 27 '18 at 13:26
1
1
@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'
– casualcoder
Nov 27 '18 at 13:40
@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'
– casualcoder
Nov 27 '18 at 13:40
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
Nov 27 '18 at 14:58
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
Nov 27 '18 at 14:58
1
1
@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)– casualcoder
Nov 27 '18 at 15:44
@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)– casualcoder
Nov 27 '18 at 15:44
thank you, your explanation is conceptual and accurate.
– hasa
Nov 27 '18 at 15:54
thank you, your explanation is conceptual and accurate.
– hasa
Nov 27 '18 at 15:54
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53498465%2fpattern-recognition-by-re-search%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
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.
– Borisu
Nov 27 '18 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.
– hasa
Nov 27 '18 at 14:48