0

I have a string of the below pattern.

string = "'L3-OPS-AB-1499', 'L3-RBP_C-449', 'L2-COM-310', 'L2-PLT-16796'"

My requirement is for a regular expression to find all the occurrences of following patterns as below

a string starting with L

followed by a number

a hyphen

then special keywords like "OPS-AB" or "PLT" or "COM"

then a hyphen

followed by a number

ex: L3-OPS-AB-1499

I tried the below regex but it gives a partial result

regex = re.search("L\d-(OPS|RBP_|-AB|C)|(COM|PLT)-\d+",string)

my output

'COM-310'

expected output

'L3-OPS-AB-1499', 'L3-RBP_C-449', 'L2-COM-310', 'L2-PLT-16796'

Any help will be appreciated, thanks

1 Answer 1

1

Use re.findall

>>> re.findall(r'L\d+-(?:OPS-AB|PLT|COM|RBP_C)-\d+', string)
['L3-OPS-AB-1499', 'L3-RBP_C-449', 'L2-COM-310', 'L2-PLT-16796']
Sign up to request clarification or add additional context in comments.

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.