Regular Expression
Steven D'Aprano
steve at pearwood.info
Thu Jun 4 09:54:24 EDT 2015
On Thu, 4 Jun 2015 11:36 pm, Palpandi wrote:
> Hi All,
>
> This is the case. To split "string2" from "string1_string2" I am using
> re.split('_', "string1_string2", 1)[1].
There is absolutely no need to use the nuclear-powered bulldozer of regular
expressions to crack that tiny peanut. Strings have a perfectly useful
split method:
py> "string1_string2".split("_")
['string1', 'string2']
> It is working fine for string "string1_string2" and output as "string2".
> But actually the problem is that if a sting is "__string1_string2" and the
> output is "_string1_string2". It is wrong.
No, the output is correct. You tell Python to split on the *first*
underscore only, which is exactly what Python does:
py> re.split('_', "__string1_string2", 1)
['', '_string1_string2']
> How to fix this issue?
Again, this is a small problem, and regular expressions are not needed. Just
strip the underscores off the left, then split:
py> s = "__string1_string2"
py> s.lstrip("_").split("_")
['string1', 'string2']
--
Steven
More information about the Python-list
mailing list