Regular expressions
Steven D'Aprano
steve at pearwood.info
Wed Nov 4 19:54:20 EST 2015
On Thu, 5 Nov 2015 10:02 am, Seymore4Head wrote:
> So far the only use I have for regex is to replace slicing, but I
> think it is an improvement.
I don't understand this. This is like saying "so far the only use I have for
a sandwich press is to replace my coffee pot". Regular expressions and
slicing do very different things.
Slicing extracts substrings, given known starting and ending positions:
py> the_str = "Now is the time for all good men..."
py> the_str[7:12]
'the t'
Regular expressions don't extract substrings with known start/end positions.
They *find* matching text, giving a search string with metacharacters. (If
there are no metacharacters in your search string, you shouldn't use a
regex. str.find will be significantly faster and more convenient.)
Slicing is not about finding text, it is about extracting text once you've
already found it. So they are complementary, not alternatives.
--
Steven
More information about the Python-list
mailing list