I think what you need is something like this. I tried to write it clear and simple:
import re
input_data = 'AB SSP\n,BCD 110/XYZ/A14/ABD1.112\nAB AAD983/*ABC/0003\nAXYZ AB *SSD* 1:23:05\nAADX'
lines = input_data.split('\n')
for line in lines:
if re.match('.*(^|\s+)AB(\s+|$).*', line):
print('AB found')
else:
print('-')
Explanation:
First we break down the text into lines, and then we iterate through those lines and check each one using a module called regular expression or re
which is used to check for all kinds of patterns in the strings.
Here, I specified that I want an AB
that is either at the start of the line or is preceded by a white space character, and is either at the end of the line or is followed by a white space character.
And this is the output:
AB found
-
AB found
AB found
-
ANSWER OF YOUR UPDATED QUESTION:
Your explanation is not very clear, but from what I understand you want to only check the first line of the input and if it contains an AB
then you want to print the 3rd and 4th line. Again, Your explanation is very ambiguous and I’m almost certain that I’m missing something, but anyway here’s the code:
import re
input_data = 'AB SSP\n,BCD 110/XYZ/A14/ABD1.112\nAB AAD983/*ABC/0003\nAXYZ AB *SSD* 1:23:05\nAADX'
lines = input_data.split('\n')
if re.match('.*(^|\s+)AB(\s+|$).*', lines[0]):
print(lines[2])
print(lines[3])
CLICK HERE to find out more related problems solutions.