I have a string (essentially an abbreviation - e.g. USA, with all letters in capitals) and a list of texts. I want to select those texts containing all the letters in the string (case-sensitive match). For example,
string = "USA"
texts = ["United States of America", "United States", "United States of America and Iraq"]
#Result shoud be:
results = ["United States of America", "United States of America and Iraq"]
I have tried with (?=U)(?=S)(?=A)
(which is what the answers to the duplicate question suggests) but this doesn't seem to work as the regex expects the letters to be occurring in exact sequence. Also, I do not want to check small letters and spaces following each of the Capitals i.e., [?=U]([a-zA-Z]*[\s]+)*[?=S]([a-zA-Z]*[\s]+)*[?=A][a-zA-Z]*
as these would be simply redundant (while not working perfectly).
What I am looking is to try with an expression equivalent to [USA]
- which instead performs an OR operation to select texts containing at least one letter of the string. Is there any expression as elegant to carry out an 'AND' operation in regex?
No comments:
Post a Comment