I have some strings like:
tool_abc
tool_abc_data
tool_xyz
tool_xyz_data
file_abc
file_xyz_data
My goal is to have an RegEx to match any strings starting with tool_
and NOT ending with _data
. How can I write one?
I have some strings like:
tool_abc
tool_abc_data
tool_xyz
tool_xyz_data
file_abc
file_xyz_data
My goal is to have an RegEx to match any strings starting with tool_
and NOT ending with _data
. How can I write one?
From https://docs.python.org/2/library/re.html#regular-expression-syntax:
(?<!...)
Matches if the current position in the string is not preceded by a match
for .... This is called a negative lookbehind assertion. Similar to positive
lookbehind assertions, the contained pattern must only match strings of some
fixed length and shouldn’t contain group references. Patterns which start
with negative lookbehind assertions may match at the beginning of the string
being searched..
I think the regex you need is '^tool_.*$(?<!_data)'
:
>>> re.match('^tool_.*$(?<!_data)', 'tool_abc')
<_sre.SRE_Match object at 0x10ef4fd98>
>>> re.match('^tool_.*$(?<!_data)', 'tool_abc_data')
>>> re.match('^tool_.*$(?<!_data)', 'tool_abc_data_file')
<_sre.SRE_Match object at 0x10ef4fe00>
>>> re.match('^tool_.*$(?<!_data)', 'tool_abc_file')
<_sre.SRE_Match object at 0x10ef4fd98>
>>> re.match('^tool_.*$(?<!_data)', 'tool_abc_data')
>>> re.match('^tool_.*$(?<!_data)', 'abc_data')
>>> re.match('^tool_.*$(?<!_data)', 'file_xyz_data')
>>> re.match('^tool_.*$(?<!_data)', 'file_xyz')
maybe something like this?:
strStart = "tool_"
strEnd = "_data"
for s in Strings_list:
if s.startswith(strStart) and not s.endswith(strEnd):
doSomething()
else:
doSomethingElse()