Huh O.O
let's see if i can handle this ;-)
We know an . dot finds one single char/sign.
So .+ finds one or as-many-as-possible till the last char/sign in the string/filename. That's how regex engines works.
An \s (if you use .+\s) finds one single withespace/blank, but only after the .+ is satisfied
And this is only if the .+ has cached all it could get. All chars, all digits, all blanks, all signs.
Yes, please note that the . dot find ANY char or sign, even whitespace blanks! That's Fact! Think like that! It's the way it works.
> would you mind to explain "greedy" by simple words.
What is simpler then "get as many as you can get" ?
Maybe:
'greedy' find the right most match.
'Lazy' with ?-sign find the left most possible match.
i will post an link to an tool so you can watch how regex engine works
Since your real filename are different from your example filenames you have seen other results then i had expected.
Your right that .+\s finds the very last blank on the right.
So use .+?\s to let the engine match the first blank from the left.
----------------------------------------------
Note:
i don't talk here about groups() because they don't matter for matching.
The groups are only good for you have somewhat you can refer back to later if you need the content of this match.
And i don't tell you that there are regex engines that works other way around like we talk about here That are for a further lesson (or read Mastering of RegEx )
----------------------------------------------
You have
1;14 (Channel1 - 30112007 1330 169 MP2192g MP2192e AC320384g)_+0+0+160.mpg.abc
You want to have
1;14 (Channel1 - 30112007 1330 169 MP2192g MP2192e AC320384g).mpg.abc
1.) We have to search for ALL till an underscore ===> .+_
2.) We have to search for ALL till the first dot ===> .+?\.
3.) We have to catch the rest ===> .+
It also works if we move the dot from part 2 to part 3 (i think you want keep the dot?)
1.) We have to search for ALL till an underscore ===> .+_
2.) We have to search for ALL ===> .+?
3.) till the first dot and catch the rest ===> \..+
I think you don't want keep the underscore but drop them?
1.) We have to search for ALL ===> .+
2.) till an underscore and search for ALL ===> _.+?
3.) till the first dot and catch the rest ===> \..+
So i try an expression like => (.+)_.+?(\..+) and replace with \1\2 ===> %f(s/(.+)_.+?(\..+)/\1\2/g)
We don't have to group the _.+? because we didn't need them later
-----------------------------------------------------------------------------------
Test it
Try
(.+)_.+(\..+) ===> %f(s/(.+)_.+(\..+)/\1\2/)
instead of
(.+)_.+?(\..+) ===> %f(s/(.+)_.+?(\..+)/\1\2/)
to see the greedy effect.
And we don't need the global 'g'-sign because we have only one occurrence of our match
-----------------------------------------------------------------------------------
> Search within Siren help didn't reveal any "global". Sorry!
OK. sorry.
Try:
1.) open Siren help with F1-key
2.) Strg+F to search
3.) search for regu
You there.
That 'g' means global i know from reading PERL RegEx manuals on the net.
-------------------------------------------------
> But why does \..+? not stop at . of .mpg?
This ?-sign you have to put to the (-how do we call this?-) last .+ group left before the \..+ group.
Because this last group should be lazy and stop before the dot we want to catch with the \.
So try .+?\..+
--------------------------------
I am afraid i didn't find the right words
Maybe you should join an real regex forum?
----------------
I hope i have covered all you questions, if not please nudge me ;-)
If you have more questions please ask.
Merry x-mas or what ever to all.
i hope no regex pro will read this stammering ever