Re: How to escape strings for re.finditer?

2023-03-02 Thread Grant Edwards
On 2023-03-02, Peter J. Holzer wrote: > [1] Personally I'd say you shouldn't use Outlook if you are reading > mails where line breaks (or other formatting) is important, but ... I'd shorten that to "You shouldn't use Outlook if mail is important." -- https://mail.python.org/mailman/listin

RE: How to escape strings for re.finditer?

2023-03-02 Thread avi.e.gross
. -Original Message- From: Python-list On Behalf Of Peter J. Holzer Sent: Thursday, March 2, 2023 3:09 PM To: python-list@python.org Subject: Re: How to escape strings for re.finditer? On 2023-03-01 01:01:42 +0100, Peter J. Holzer wrote: > On 2023-02-28 15:25:05 -0500, avi.e

Re: How to escape strings for re.finditer?

2023-03-02 Thread Peter J. Holzer
On 2023-03-01 01:01:42 +0100, Peter J. Holzer wrote: > On 2023-02-28 15:25:05 -0500, avi.e.gr...@gmail.com wrote: > > I had no doubt the code you ran was indented properly or it would not work. > > > > I am merely letting you know that somewhere in the process of copying > > the code or the transi

Re: How to escape strings for re.finditer?

2023-03-01 Thread Thomas Passin
On 3/1/2023 12:04 PM, Grant Edwards wrote: On 2023-02-28, Cameron Simpson wrote: Regexps are: - cryptic and error prone (you can make them more readable, but the notation is deliberately both terse and powerful, which means that small changes can have large effects in behaviour); the "

Re: How to escape strings for re.finditer?

2023-03-01 Thread Grant Edwards
On 2023-02-28, Cameron Simpson wrote: > Regexps are: > - cryptic and error prone (you can make them more readable, but the >notation is deliberately both terse and powerful, which means that >small changes can have large effects in behaviour); the "error prone" >part does not mean

RE: How to escape strings for re.finditer?

2023-02-28 Thread avi.e.gross
Of Peter J. Holzer Sent: Tuesday, February 28, 2023 7:26 PM To: python-list@python.org Subject: Re: How to escape strings for re.finditer? On 2023-03-01 01:01:42 +0100, Peter J. Holzer wrote: > On 2023-02-28 15:25:05 -0500, avi.e.gr...@gmail.com wrote: > > It happens to be easy for me to

Re: How to escape strings for re.finditer?

2023-02-28 Thread Weatherby,Gerard
@python.org Subject: Re: How to escape strings for re.finditer? *** Attention: This is an external email. Use caution responding, opening attachments or clicking on links. *** Using str.startswith is a cool idea in this case. But is it better than regex for performance or reliability? Regex

Re: How to escape strings for re.finditer?

2023-02-28 Thread Peter J. Holzer
On 2023-03-01 01:01:42 +0100, Peter J. Holzer wrote: > On 2023-02-28 15:25:05 -0500, avi.e.gr...@gmail.com wrote: > > It happens to be easy for me to fix but I sometimes see garbled code I > > then simply ignore. > > Truth to be told, that's one reason why I rarely read your mails to the > end. Th

Re: How to escape strings for re.finditer?

2023-02-28 Thread Peter J. Holzer
On 2023-02-28 15:25:05 -0500, avi.e.gr...@gmail.com wrote: > Jen, > > > > I had no doubt the code you ran was indented properly or it would not work. > > > > I am merely letting you know that somewhere in the process of copying > the code or the transition between mailers, my version is mes

Re: How to escape strings for re.finditer?

2023-02-28 Thread Cameron Simpson
On 28Feb2023 18:57, Jen Kris wrote: One question:  several people have made suggestions other than regex (not your terser example with regex you shown below).  Is there a reason why regex is not preferred to, for example, a list comp?  These are different things; I'm not sure a comparison is

Re: How to escape strings for re.finditer?

2023-02-28 Thread Thomas Passin
On 2/28/2023 2:40 PM, David Raymond wrote: With a slight tweak to the simple loop code using .find() it becomes a third faster than the RE version though. def using_simple_loop2(key, text): matches = [] keyLen = len(key) start = 0 while (foundSpot := text.find(key, start))

RE: How to escape strings for re.finditer?

2023-02-28 Thread avi.e.gross
function that hides the loops inside a faster environment than the interpreter. -Original Message- From: Python-list On Behalf Of David Raymond Sent: Tuesday, February 28, 2023 2:40 PM To: python-list@python.org Subject: RE: How to escape strings for re.finditer? > I wrote my previ

RE: How to escape strings for re.finditer?

2023-02-28 Thread avi.e.gross
To: python-list@python.org Subject: Re: How to escape strings for re.finditer? 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

RE: How to escape strings for re.finditer?

2023-02-28 Thread avi.e.gross
, February 28, 2023 12:58 PM To: avi.e.gr...@gmail.com Cc: 'Python List' Subject: RE: How to escape strings for re.finditer? The code I sent is correct, and it runs here. Maybe you received it with a carriage return removed, but on my copy after posting, it is correct: example = '

RE: How to escape strings for re.finditer?

2023-02-28 Thread David Raymond
> I wrote my previous message before reading this.  Thank you for the test you > ran -- it answers the question of performance.  You show that re.finditer is > 30x faster, so that certainly recommends that over a simple loop, which > introduces looping overhead.  >>     def using_simple_loop(

Re: How to escape strings for re.finditer?

2023-02-28 Thread Thomas Passin
On 2/28/2023 11:48 AM, Jon Ribbens via Python-list wrote: On 2023-02-28, Thomas Passin wrote: ... It is interesting, though, how pre-processing the search pattern can improve search times if you can afford the pre-processing. Here's a paper on rapidly finding matches when there may be up to

Re: How to escape strings for re.finditer?

2023-02-28 Thread Thomas Passin
list On Behalf Of Jen Kris via Python-list Sent: Monday, February 27, 2023 8:36 PM To: Cameron Simpson Cc: Python List Subject: Re: How to escape strings for re.finditer? I haven't tested it either but it looks like it would work. But for this case I prefer the relative simplicity o

RE: How to escape strings for re.finditer?

2023-02-28 Thread avi.e.gross
: python-list@python.org Subject: Re: How to escape strings for re.finditer? Op 28/02/2023 om 3:44 schreef Thomas Passin: > On 2/27/2023 9:16 PM, avi.e.gr...@gmail.com wrote: >> And, just for fun, since there is nothing wrong with your code, this >> minor change is terser: >&g

Re: How to escape strings for re.finditer?

2023-02-28 Thread Thomas Passin
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 real

Re: How to escape strings for re.finditer?

2023-02-28 Thread Jon Ribbens via Python-list
On 2023-02-28, Thomas Passin wrote: > On 2/28/2023 10:05 AM, Roel Schroeven wrote: >> Op 28/02/2023 om 14:35 schreef Thomas Passin: >>> On 2/28/2023 4:33 AM, Roel Schroeven wrote: [...] (2) Searching for a string in another string, in a performant way, is not as simple as it first

Re: How to escape strings for re.finditer?

2023-02-28 Thread Jen Kris via Python-list
I wrote my previous message before reading this.  Thank you for the test you ran -- it answers the question of performance.  You show that re.finditer is 30x faster, so that certainly recommends that over a simple loop, which introduces looping overhead.  Feb 28, 2023, 05:44 by li...@tompass

Re: How to escape strings for re.finditer?

2023-02-28 Thread Jen Kris via Python-list
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.  Feb 27, 2023, 18:52 by li...@tompassin.net: > On 2/27/2023 9:16 PM, avi.e.gr...@gmail.com

RE: How to escape strings for re.finditer?

2023-02-28 Thread Jen Kris via Python-list
s but also show > the exact text that matched or even show some characters before and/or after > for context. > > > -Original Message- > From: Python-list On > Behalf Of Jen Kris via Python-list > Sent: Monday, February 27, 2023 8:36 PM > To: Cameron Simpson

Re: How to escape strings for re.finditer?

2023-02-28 Thread Thomas Passin
On 2/28/2023 10:05 AM, Roel Schroeven wrote: Op 28/02/2023 om 14:35 schreef Thomas Passin: On 2/28/2023 4:33 AM, Roel Schroeven wrote: [...] (2) Searching for a string in another string, in a performant way, is not as simple as it first appears. Your version works correctly, but slowly. In so

Re: How to escape strings for re.finditer?

2023-02-28 Thread Roel Schroeven
Op 28/02/2023 om 14:35 schreef Thomas Passin: On 2/28/2023 4:33 AM, Roel Schroeven wrote: [...] (2) Searching for a string in another string, in a performant way, is not as simple as it first appears. Your version works correctly, but slowly. In some situations it doesn't matter, but in other

Re: How to escape strings for re.finditer?

2023-02-28 Thread Thomas Passin
On 2/28/2023 4:33 AM, Roel Schroeven wrote: Op 28/02/2023 om 3:44 schreef Thomas Passin: On 2/27/2023 9:16 PM, avi.e.gr...@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 ma

Re: How to escape strings for re.finditer?

2023-02-28 Thread Roel Schroeven
Op 28/02/2023 om 3:44 schreef Thomas Passin: On 2/27/2023 9:16 PM, avi.e.gr...@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'

RE: How to escape strings for re.finditer?

2023-02-27 Thread avi.e.gross
rting from where you left off. -Original Message- From: Python-list On Behalf Of Thomas Passin Sent: Monday, February 27, 2023 9:44 PM To: python-list@python.org Subject: Re: How to escape strings for re.finditer? On 2/27/2023 9:16 PM, avi.e.gr...@gmail.com wrote: > And, just for fun

Re: How to escape strings for re.finditer?

2023-02-27 Thread Thomas Passin
On 2/27/2023 9:16 PM, avi.e.gr...@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(), ma

RE: How to escape strings for re.finditer?

2023-02-27 Thread avi.e.gross
bookkeeper. In those cases, you may want even more than offsets but also show the exact text that matched or even show some characters before and/or after for context. -Original Message- From: Python-list On Behalf Of Jen Kris via Python-list Sent: Monday, February 27, 2023 8:36 PM To:

RE: How to escape strings for re.finditer?

2023-02-27 Thread avi.e.gross
tion and now you have a tool. Even better, you can make it return whatever you want. -Original Message- From: Python-list On Behalf Of Jen Kris via Python-list Sent: Monday, February 27, 2023 7:40 PM To: Bob van der Poel Cc: Python List Subject: Re: How to escape strings for re.findit

Re: How to escape strings for re.finditer?

2023-02-27 Thread Jen Kris via Python-list
I haven't tested it either but it looks like it would work.  But for this case I prefer the relative simplicity of: example = 'X - abc_degree + 1 + qq + abc_degree + 1' find_string = re.escape('abc_degree + 1') for match in re.finditer(find_string, example):     print(match.start(), match.end())

Re: How to escape strings for re.finditer?

2023-02-27 Thread Cameron Simpson
On 28Feb2023 00:57, Jen Kris wrote: Yes, that's it.  I don't know how long it would have taken to find that detail with research through the voluminous re documentation.  Thanks very much.  You find things like this by printing out the strings you're actually working with. Not the original s

Re: How to escape strings for re.finditer?

2023-02-27 Thread Cameron Simpson
On 28Feb2023 01:13, Jen Kris wrote: I went to the re module because the specified string may appear more than once in the string (in the code I'm writing). Sure, but writing a `finditer` for plain `str` is pretty easy (untested): pos = 0 while True: found = s.find(substring,

Re: How to escape strings for re.finditer?

2023-02-27 Thread Jen Kris via Python-list
string.count() only tells me there are N instances of the string; it does not say where they begin and end, as does re.finditer.  Feb 27, 2023, 16:20 by bobmellow...@gmail.com: > Would string.count() work for you then? > > On Mon, Feb 27, 2023 at 5:16 PM Jen Kris via Python-list <> > python-l

RE: How to escape strings for re.finditer?

2023-02-27 Thread avi.e.gross
age- From: Python-list On Behalf Of Jen Kris via Python-list Sent: Monday, February 27, 2023 7:14 PM To: Cameron Simpson Cc: Python List Subject: Re: How to escape strings for re.finditer? I went to the re module because the specified string may appear more than once in the string

Re: How to escape strings for re.finditer?

2023-02-27 Thread Jen Kris via Python-list
I went to the re module because the specified string may appear more than once in the string (in the code I'm writing).  For example:  a = "X - abc_degree + 1 + qq + abc_degree + 1"  b = "abc_degree + 1"  q = a.find(b) print(q) 4 So it correctly finds the start of the first instance, but not

RE: How to escape strings for re.finditer?

2023-02-27 Thread avi.e.gross
the backslash breaks the match from going further. -Original Message- From: Python-list On Behalf Of MRAB Sent: Monday, February 27, 2023 6:46 PM To: python-list@python.org Subject: Re: How to escape strings for re.finditer? On 2023-02-27 23:11, Jen Kris via Python-list wrote: > When matching

Re: How to escape strings for re.finditer?

2023-02-27 Thread Jen Kris via Python-list
Yes, that's it.  I don't know how long it would have taken to find that detail with research through the voluminous re documentation.  Thanks very much.  Feb 27, 2023, 15:47 by pyt...@mrabarnett.plus.com: > On 2023-02-27 23:11, Jen Kris via Python-list wrote: > >> When matching a string against

Re: How to escape strings for re.finditer?

2023-02-27 Thread Cameron Simpson
On 28Feb2023 00:11, Jen Kris wrote: When matching a string against a longer string, where both strings have spaces in them, we need to escape the spaces.  This works (no spaces): import re example = 'abcdefabcdefabcdefg' find_string = "abc" for match in re.finditer(find_string, example):    

Re: How to escape strings for re.finditer?

2023-02-27 Thread MRAB
On 2023-02-27 23:11, Jen Kris via Python-list wrote: When matching a string against a longer string, where both strings have spaces in them, we need to escape the spaces. This works (no spaces): import re example = 'abcdefabcdefabcdefg' find_string = "abc" for match in re.finditer(find_string,