How to find the nth occurence of a string
Eric Bouyoux
eric.bouyoux at insidefr.com
Wed Oct 18 12:34:35 CEST 2006
Tony Balinski a écrit :
Thank you for all these detailed explanations.
Eric.
Quoting Eric Bouyoux [1]<eric.bouyoux at insidefr.com>:
Tony Balinski a écrit :
Quoting Eric Bouyoux [1][2]<eric.bouyoux at insidefr.com>:
Hi,
Has anybody already written a macro that help to find the nth
occurence of a string ?
You can almost do it with a regex: given N as a decimal, searching for
(?:(?n.*?)(?:SearchString)){N}
Hi,
Is anybody able to explain me this regexp. I thought I was able to
understand regular expression but it seems I'm not.
Regards.
Eric.
The (?:RE) is for grouping without capturing. In other words, you can
add the repeat operators to stuff like *, +, ?, {1,5} etc to apply to
"RE" rather than the last "E", and the result will not be available
in a \1 later in the regex or the replacement.
The (?nRE) also groups without capturing, but also allows you to treat
\n like any other character (so .* won't match to the end of line, but
all the way to the end of document or string).
The *? construct is a lazy (rather than greedy) repetition. The matching
stops as soon as possible rather than as late as possible, eg
input string: "this is a string to see how good my regex is."
RE ".*is" matches: "this is a string to see how good my regex is"
RE ".*?is" matches: "this"
Finally, the {N} construct is the same as {N,N}, a special case of {m,n}
which means match at least m repetitions and at most n (for decimal
values N, m and n); so {N} means "match exactly N repetitions of the
preceding regex"
So the above regex says match exactly N repetitions of text not including
"SearchString" (which could be a regex itself) followed by "SearchString",
and spanning as many lines as you like.
Read NEdit's help information from the menu at:
- Help > Regular Expressions > Parenthetical Constructs
for ( ), (?: ), (?= ), (?! ), (?<= ), (?<! ), (?i ), (?I ), (?n ), (?N )
- Help > Regular Expressions > Basic Regular Expression Syntax
for Quantifiers *, +, ?, *?, +?, ??, {m,n}
Tony
References
1. mailto:eric.bouyoux at insidefr.com
2. mailto:eric.bouyoux at insidefr.com
More information about the Discuss
mailing list