Non-Greedy n through m (how to use)?

General discussion related to "Everything".
Post Reply
Debugger
Posts: 630
Joined: Thu Jan 26, 2017 11:56 am

Non-Greedy n through m (how to use)?

Post by Debugger »

How to search for a specific number of characters?
^={0,1}$
Non-Greedy n through m
{n,m}?

Found:
=


And how to find, e.g.
==
===
====
or more?
void
Developer
Posts: 16897
Joined: Fri Oct 16, 2009 11:31 pm

Re: Non-Greedy n through m (how to use)?

Post by void »

With regex enabled, please try searching for:
={1,3}?

where:
{x,y} = match the preceding element x - y times.
1 is the minimum number of characters to match.
3 is the maximum number of consecutive characters to match.
? = nongreedy

Wrap your search with ^ and $ to match only = characters.
There really needs to be something to match after the ? to make the non-greedy search useful.
In Everything I use (.*?)\(.*?) to match anything and break at the first \ (non-greedy)

https://stackoverflow.com/questions/3075130/what-is-the-difference-between-and-regular-expressions
therube
Posts: 5003
Joined: Thu Sep 03, 2009 6:48 pm

Re: Non-Greedy n through m (how to use)?

Post by therube »

Didn't realize, wasn't sure if you could do that.
I thought, wasn't sure, if it only applied to .* and .+

But it looks like it is doing, something.


File:
ozzy_osbourne_blizzard_of_ozz_cd.jpg

Compare:

greedy, on the last item

Code: Select all

regex:(z)\1{1,2}?.*?\1{1,2}
zzy_osbourne_blizz
vs

non-greedy, on the last item

Code: Select all

regex:(z)\1{1,2}?.*?\1{1,2}?
zzy_osbourne_bliz
Debugger
Posts: 630
Joined: Thu Jan 26, 2017 11:56 am

Re: Non-Greedy n through m (how to use)?

Post by Debugger »

Incorrect pattern because it matches all characters!

Code: Select all

={1,3}?
Found:15 matches

Code: Select all

=
==
===
====
=====
therube
Posts: 5003
Joined: Thu Sep 03, 2009 6:48 pm

Re: Non-Greedy n through m (how to use)?

Post by therube »

Note the differences, anchored or not, & greedy or not.

Without the anchor, non-greedy, I suppose it finds solely "=", then if searches for more, the same way, & finds any additional "=" - if they exist.
So in that respect, it appears as if it is finding "all" =, where in fact it is finding a single {1,3}? "=", but multiple times.

Once the anchor is added in, you can "see" that it is only finding the initial =, with the search, even though you have {1,3}, ending at that point - because non-greedy.

Remove non-greedy, but keep the anchor, & "=" or "==" or "===" are matched, greedily, but no more then that.
.
.
Everything - regex anchored or not non-greedy.png
Everything - regex anchored or not non-greedy.png (20.12 KiB) Viewed 8226 times
Debugger
Posts: 630
Joined: Thu Jan 26, 2017 11:56 am

Re: Non-Greedy n through m (how to use)?

Post by Debugger »

Invalid pattern because it finds more characters, and a specific number of characters is given:

^(=){3,3}

Correct:===
Wrong:====
Wrong:=============


Working:
^(=){3,3}$
therube
Posts: 5003
Joined: Thu Sep 03, 2009 6:48 pm

Re: Non-Greedy n through m (how to use)?

Post by therube »

Well, ^(=){3,3} is correct, all your results are correct, it's only that you do not want a result included if it is followed by (one or more) additional =.

And yes, ^(=){3,3}$ will meet your criteria.

I tried something like ^(=){1,3}[^={4,}], kind of to mean = or == or ===, but not followed by any further = characters, but my thoughts/syntax haven't panned out to accomplish that, yet...
Debugger
Posts: 630
Joined: Thu Jan 26, 2017 11:56 am

Re: Non-Greedy n through m (how to use)?

Post by Debugger »

so more than = are not allowed

Example:
^={1,3}(?![=]+)
Post Reply