Page 1 of 1

Simple regex not capturing as expected

Posted: 29 Jul 2015 18:48
by Papoulka
I had some files of this form:
  • abcd[xyz].txt
where the strings could be any alpha-numeric sequences.
I wanted to remove the material between the brackets and thought the following RegExp Rename would do it:

Code: Select all

^([^\[]*) > $1.txt
However, instead of returning "abcd", the $1 returns the entire original filename.

XY is not alone in this behavior. The Regex101 website (with g,m option set) does the same thing. So does an old copy of Regex Designer. The latter two show the capture groups visibly and both are gathering only the "abcd" - but they return $1 as the whole string. That's what puzzles me.

In contrast, RegexBuddy3 (with JavaScript+VBScript option) returns $1 as "abcd", as I would expect.

I went ahead and fixed those files manually, but removing bracket material is a reasonable task. Does anyone know (1) how to do it in an XY regex and (B) why the regex I tried gives such strange results in several engines?

Re: Simple regex not capturing as expected

Posted: 29 Jul 2015 19:10
by highend
I wanted to remove the material between the brackets
One way:

Code: Select all

^(.*(?=\[))\[([^]]+)](.*$) > $1[].txt

Re: Simple regex not capturing as expected

Posted: 30 Jul 2015 07:32
by bdeshi
or

Code: Select all

\[.*?\](\.[^\.]+)$ > $1
(B) why the regex I tried gives such strange results in several engines?

Code: Select all

 ^([^\[]*) > $1.txt
  |      |
  |     /
  |    /
  |   /
  abcd[xyz].txt
only "abcd" matches the given pattern, and only this matching section is replaced by $1.txt == abcd.txt
The remaining "[xyz].txt" is not included in the pattern, and remains as-is. (You could've included this part with a .* after the pattern)
So you end up with "abcd.txt[xyz].txt"

Re: Simple regex not capturing as expected

Posted: 30 Jul 2015 15:26
by Papoulka
Thanks to both for the detailed answers.

But Sammay I am still confused especially with regard to Regexp Rename. If my file is "zapple.txt" and I apply

Code: Select all

^(z)apple > $1
I get "z.txt". The "apple" part is not included in the pattern and is not kept as-is. This seems contrary to your explanation. OTOH the ".txt" extension is kept as-is even though it was not specifically captured and transferred... which BTW seems contrary to the Help statement that "Renaming includes file extensions".

Regexp Rename is a tremendously useful command, but its Help could use more explanation on scope and a few more common examples. The only ones relate to changing an extension, which is useful but certainly a less common case.

Re: Simple regex not capturing as expected

Posted: 30 Jul 2015 16:20
by bdeshi
Papoulka wrote:Thanks to both for the detailed answers.

But Sammay I am still confused especially with regard to Regexp Rename. If my file is "zapple.txt" and I apply

Code: Select all

^(z)apple > $1
I get "z.txt". The "apple" part is not included in the pattern and is not kept as-is.
of course "apple" IS included in the pattern. You typed it in.

maybe you're confusing the terms capturing group, pattern and string?
The whole complete ALL of "(z)apple" is the regexp matching pattern.
"(z)" is a capturing group, anything that matches is stored for $1,
However, the remaining "apple" is still part of the regexp pattern.

In the string "zappletime", the "(z)apple" pattern matches the bold part. The underlined part is additionally recorded as $1.
and any other part of the string that did not match the pattern is in italics.
Then the whole matching part ("zapple"), and only the matching part, is replaced by $1 == z, but the non-matching areas remain untouched.
So you end up with "ztime" (naptime?) :mrgreen:

Likewise, in the case of "zapple.txt", that pattern matches like this: zapple.txt => $1 => z.txt (.txt did not match and is not replaced)

Re: Simple regex not capturing as expected

Posted: 30 Jul 2015 18:04
by Papoulka
Sammy - you're right that I was confusing the terms. And I went further astray in RegexBuddy3 where I used "List Replacements" instead of "Search and Replace All Matches". By chance that seemed to confirm my wrong notion.

To paraphrase your conclusion, when using Regexp Rename: The whole matching part, and only the matching part, is replaced by the capture group(s) e.g. $1, $2 etc and anything added to them. The non-matching parts are unchanged.

That statement (and a few examples illustrating the idea) would be very useful in the Help, which is currently too brief for such an important feature.

Thanks for your very detailed explanation. I'm surprised at how much I've done with this command without fully understanding it!

Re: Simple regex not capturing as expected

Posted: 30 Jul 2015 20:01
by bdeshi
That statement (and a few examples illustrating the idea) would be very useful in the Help, which is currently too brief for such an important feature.
That statement is actually a basic universal rule of regexp replacement. It's not really in the scope of XYplorer's help, unless Don decides to teach regexp. :kidding:
If you look at the help/reference of any other program (even most big huge apps) that utilizes regexp, you'll see 95% of the time that basically all they say on RegExp is "See http://regular-expressions.info/ for more" or some such.

Re: Simple regex not capturing as expected

Posted: 30 Jul 2015 20:08
by highend
I don't think that the help file is the right way to teach (basic) regexpressions, too.

I've never seen a better tutorial than on http://www.regular-expressions.info/tutorial.html and I'd recommend it to everyone who's interested. Mh, I should get an affiliate link for http://www.regexbuddy.com/^^