How to escape strings for re.finditer?
Thomas Passin
list1 at tompassin.net
Tue Feb 28 13:30:32 EST 2023
On 2/28/2023 1:07 PM, Jen Kris wrote:
>
> Using str.startswith is a cool idea in this case. But is it better than
> regex for performance or reliability? Regex syntax is not a model of
> simplicity, but in my simple case it's not too difficult.
The trouble is that we don't know what your case really is. If you are
talking about a short pattern like your example and a small text to
search, and you don't need to do it too often, then my little code
example is probably ideal. Reliability wouldn't be an issue, and
performance would not be relevant. If your case is going to be much
larger, called many times in a loop, or be much more complicated in some
other way, then a regex or some other approach is likely to be much faster.
> Feb 27, 2023, 18:52 by list1 at tompassin.net:
>
> On 2/27/2023 9:16 PM, avi.e.gross at gmail.com wrote:
>
> And, just for fun, since there is nothing wrong with your code,
> this minor change is terser:
>
> example = 'X - abc_degree + 1 + qq + abc_degree + 1'
> for match in re.finditer(re.escape('abc_degree + 1')
> , example):
>
> ... print(match.start(), match.end())
> ...
> ...
> 4 18
> 26 40
>
>
> Just for more fun :) -
>
> Without knowing how general your expressions will be, I think the
> following version is very readable, certainly more readable than
> regexes:
>
> example = 'X - abc_degree + 1 + qq + abc_degree + 1'
> KEY = 'abc_degree + 1'
>
> for i in range(len(example)):
> if example[i:].startswith(KEY):
> print(i, i + len(KEY))
> # prints:
> 4 18
> 26 40
>
> If you may have variable numbers of spaces around the symbols, OTOH,
> the whole situation changes and then regexes would almost certainly
> be the best approach. But the regular expression strings would
> become harder to read.
> --
> https://mail.python.org/mailman/listinfo/python-list
>
>
More information about the Python-list
mailing list