I have a string
, an array
which contains possible end characters for that string, and a block of text to parse. For example:
stringText = "something"
endChars = [",", ".", ";", " "]
textBlock = "This string may contain something;"
In a one line if
statement, I want to check if textBlock
contains the stringText
followed by any one of the endChars
. I'm pretty sure I can do this with the built in any
function in Python 2.7, but my efforts so far have failed. I have something similar to this:
if re.search(stringText + any(endChar in endChars), textBlock, re.IGNORECASE):
print("Match")
I've seen this post, however I'm struggling to apply it to my check above. Any help doing to would be appreciated.
EDIT:
In addition to the above, is it possible to determine which of the endChars
was found in the string? Using @SCB's answer below and adapting it, I would expect the following to do exactly that, but it throws an undefined error.
stringText = "something"
endChars = [",", ".", ";", " "]
textBlock = "This string may contain something;"
if any((stringText + end).lower() in textBlock.lower() for end in endChars):
print("Match on " + end)
Expected output: Match on ;
Actual output NameError: name 'end' is not defined
UPDATE I have arrived at a suitable solution to this problem, at least for my requirements. It's not a one-liner, but it does the job. For completeness, shown below
for end in endChars:
if stringText + end in textBlock:
print("Match on " + end)