#1 2008-04-16 23:17

eR@SeR
Senior Member
From: Земун, Србија
Registered: 2008-01-23
Posts: 353

Examples of Regular Expressions

1. If you have your own Regular Expressions (patterns) let's share with me (us).


2. Have some obscurity about Regular Expressions? You are in right place!
Feel free to visit www.regular-expressions.info or post your question(s) here and some of us will help you smile.

Edit:

Also, you can find here http://www.regexpstudio.com/TRegExpr/He … yntax.html
more about Syntax of Regular Expressions.

Regular Expressions:



1. Expression: "([a-z])([A-Z])"            (separates lower from Upper case in filename)
    Replace: "$1 $2"
case-sensitive is checked
extension is skipped (default)

Examples: Tute-WealthLab-English.rar ---> Tute-Wealth Lab-English.rar
Armadillo 2.00 (ThreePage + CopyMem 2).rar ---> Armadillo 2.00 (Three Page + Copy Mem 2).rar



2. Expression: "\A[\d\-\.\s]+(.+)"       (removes digits, dash, dot, and space at the beginning of the filename)
    Replace: "$1"                          Taken from: http://www.den4b.com/forum/viewtopic.php?id=431

Examples: 008. Madonna - Like A Prayer.wma, 08 Madonna - Like A Prayer.wma,
08. Madonna - Like A Prayer.wma, 08.-Madonna - Like A Prayer.wma, Madonna - Like A Prayer.wma ---> Madonna - Like A Prayer.wma



3. Expression: "(.+) (.+)"        (switches first and second word in filename)
    Replace: "$2 $1"

Examples: Charlotte Banding.jpg ---> Banding Charlotte.jpg, Desmond Mason.jpg ---> Mason Desmond.jpg

Caution: Works only for these examples when you have 2 words. With this example you can make dozens of small changes (by adding a dash as separator etc.)...

P.S. I dedicate this topic to mine promotion into senior member smile.

Last edited by eR@SeR (2008-04-18 15:27)


TRUTH, FREEDOM, JUSTICE and FATHERLAND are the highest morale values which human is born, lives and dies for!

Offline

#2 2008-04-17 11:37

krtek
Senior Member
From: Łódź (Poland)
Registered: 2008-02-21
Posts: 262

Re: Examples of Regular Expressions

eR@SeR wrote:

2. Expression: "\A[\d\-\.\s]+(.+)"       (removes digits, dash, dot, and space at the beginning of the filename)

I prefer version (which, from point of RegEx means exactly the same - just a bit different notation):
Expression: "^[-.\d\s]+(.+)"


eR@SeR wrote:

3. Expression: "(.+) (.+)"        (switches first and second word in filename)
    Replace: "$2 $1"

I would prefer using "\w" (any alfanumeric sign) instead of "." (any sign) and \s (any white sign) instead of " "
Expression: "(\w+)\s(\w+)"
Replace: "$2 $1"

If you have more than one word you simply has to repeat "\w+" for every word you have, so
Expression: "(\w+)\s(\w+\s\w+)"
Replace: "$2 $1"
will make "One Two Three" --> "Two Three One"                        (One) (Two Three) ----> (Two Three) (One)

Expression: "(\w+\s\w+)\s(\w+)"
Replace: "$2 $1"
will make "One Two Three" --> "Three One Two"                         (One Two) (Three) ---> (Three) (One Two)       

As you can see $1..$9 are bounded to contents of round brackets. $1 is for first brackets from the left, $2 for second and so on.

Try to experiment with brackets:
Expression: "(\w+)\s(\w+)\s(\w+)"
Replace: "$2 $1 $3"
will make "One Two Three" --> "Two One Three"

I hope that will make problems with swaping words clear.

Ps. Welcome in Senior Members big_smile    I knew you were just behind me.

Last edited by krtek (2008-04-18 01:11)


Regular Expressions are not as hard to understand as you may think. Check ReNamer's manual or nice Regular Expressions tutorial for more info and start to use full power of applications that use them (like ReNamer, Mp3Tag and so on).

Offline

#3 2008-04-17 12:37

eR@SeR
Senior Member
From: Земун, Србија
Registered: 2008-01-23
Posts: 353

Re: Examples of Regular Expressions

I prefer version (which, from point of RegEx means exactly the same - just a bit different notation):
Expression: "^[-.\d\s]+(.+)"

Keep going to research all possible combinations of RegEx wink! Nice one big_smile

I would prefer using "\w" (any alfanumeric sign) instead of "." (any sign) and \s (any white sign) instead of " "
Expression: "(\w+)\s(\w+)"
Replace: "$2 $1"

Or this one... Mine example is simple as it can be. And of course I don't know meaning of some expressions yet, so I made the testings and it shows like I wanted to be. roll. Anyway, thank you again for describing what any of them means smile.

Hm, I think I get it hmm

Try to experiment with brackets:
Expression: "(\w+)\s(\w+)\s(\w+)"
Replace: "$2 $1 $3"
will make "One Two Three" --> "Two One Three"

Can you explain me this example? 50 Cent - In Da Club (Rmx).mp3 ---> 50 Cent - Da In Club (Rmx).mp3
Why it's not interpreted "50 Cent" as first two words?

Ps. Welcome in Senior Members

Thank you cool

P.S. You have very good vocabulary and choose appropriate words for explaining!!!
Outstanding big_smile

Last edited by eR@SeR (2008-04-17 18:08)


TRUTH, FREEDOM, JUSTICE and FATHERLAND are the highest morale values which human is born, lives and dies for!

Offline

#4 2008-04-17 13:12

krtek
Senior Member
From: Łódź (Poland)
Registered: 2008-02-21
Posts: 262

Re: Examples of Regular Expressions

eR@SeR wrote:

50 Cent - In Da Club (Rmx).mp3 ---> 50 Cent - Da In Club (Rmx).mp3

Here you go:

Regex is lookin for THREE words (words or numbers to be excact) so it finds (50) (Cent) (-) - oops that's not three words as "-" is not an alphanumeric sign.
Then it goes further and checks (Cent) (-) (Da) - still not three words.
And finally it finds (In) (Da) (Club) - Hurray that's three words so it can apply the rule for them --> Da In Club
And then it goes further and finds (Rmx) which is not a word (because of brackets-as you're looking for words separated with \s).
So if you had "50 Cent - Da In Club One Two Three.mp3"  --->  "50 Cent - In Da Club Two One Three.mp3"
in ReNamer Regex doesn't stop after first find, it looks for the second and so on. That's why [a-z][A-Z] works no matter how many words do you have in a row.

But if you apply your version of expression "(.+) (.+) (.+)" the result will be suprise for you:
50 Cent - In Da Club (Rmx).mp3 ---> Club 50 Cent - In Da (Rmx).mp3

It's because + sign want to get as much as it can. And as long as space is "any sign" too. It first takes (50 Cent - In Da Club (Rmx)) and then it realizes that there are no more words. So it goes back. (50 Cent - In Da Club) ((Rmx)). Now it has only two.
And finally it takes (50 Cent - In Da) (Club) ((Rmx)) - and this is swapped to $2 $1 $3 as you can see above.

If you would like to treat (Rmx) as a word and get the rigth result you should use [^\s] ("not a white sign - space, end of line, tab")
Expression: ([^\s]+)\s([^\s]+)\s([^\s]+)
Replace $2 $1 $3

It will give you "50 Cent - In Da Club (Rmx).mp3" --> Cent 50 - Da In Club (Rmx).mp3
and to make it more clear:
"50 - Cent                              "- 50 Cent" 
(Rmx) In Da Club.mp3"  -->    "In (Rmx) Da Club.mp3"
So now it swaps anything that is not a space.

If you put anything else into square brackets after ^sign it will omit these signs as well, so ([^-.\s]+) will match sequence of signs that are not space, "-" or ".",     eg. "ble" "never_told"  "+you%bet#it@does", but it won't match "never-ever" or "the_end.".

I hope I haven't scared you. If you've got any other question don't hesitate to ask.
And experiment.
tongue

Last edited by krtek (2008-04-18 01:07)


Regular Expressions are not as hard to understand as you may think. Check ReNamer's manual or nice Regular Expressions tutorial for more info and start to use full power of applications that use them (like ReNamer, Mp3Tag and so on).

Offline

#5 2008-04-17 13:35

eR@SeR
Senior Member
From: Земун, Србија
Registered: 2008-01-23
Posts: 353

Re: Examples of Regular Expressions

Regex is lookin for THREE words (words or numbers to be excact) so it finds (50) (Cent) (-) - oops that's not three words as "-" is not an alphanumeric sign.

Ahaaa, so that's it. Yes, the explanation is simple. It does what you told him to do, not what you wanted to do sad. Like programming... big_smile

I hope I haven't scared you. If you've got any other question don't hesitate to ask.
And experiment.

Oh pal, I got a real headache now :-S. Better for me to go to bed and think about everything you wrote... yikes
This is joke off course tongue. I'll not hesitate to ask a question or a task for you... Rest you too now, it will be days for more discussion about this topic cool


TRUTH, FREEDOM, JUSTICE and FATHERLAND are the highest morale values which human is born, lives and dies for!

Offline

#6 2008-08-06 17:18

eR@SeR
Senior Member
From: Земун, Србија
Registered: 2008-01-23
Posts: 353

Re: Examples of Regular Expressions

Regular Expressions:
.
.
.
.
4. Expression: "\A(\s*)(.*)"            (removes space(s) at the beginning of filename)
    Replace: "$2"                            Taken from: Remove leading spaces from side of filename



5. Expression : "(\d)([a-z])"        Expression : "([a-z])(\d)"          (separates words from digits)
    Replace: "$1 $2"                    Replace: "$1 $2"

Examples: 1990porsche911carrera2guardsred0001a.jpg ---> 1990 porsche 911 carrera 2 guardsred 0001 a.jpg
                1990porsche911carrera2guardsred0001j.jpg ---> 1990 porsche 911 carrera 2 guardsred 0001 j.jpg

Last edited by eR@SeR (2008-08-11 17:04)


TRUTH, FREEDOM, JUSTICE and FATHERLAND are the highest morale values which human is born, lives and dies for!

Offline

#7 2008-08-07 21:57

krtek
Senior Member
From: Łódź (Poland)
Registered: 2008-02-21
Posts: 262

Re: Examples of Regular Expressions

If you need to delete (or replace) something from the end of filename you need to use  $ sign (lets name it as "end of string anchor") instead of ^ ("the beginning of string anchor").
So to delete "-" only from the end of filename (eg. from ReNamer- but not from Re-Namer)
you need following expression:

Expression: "-$"
Replace: ""

or

Expression: "(.+)-$"
Replace: "$1"

They both do exactly the same ;P


Just remember to have "skip extention" checked.

Last edited by krtek (2008-08-07 21:58)


Regular Expressions are not as hard to understand as you may think. Check ReNamer's manual or nice Regular Expressions tutorial for more info and start to use full power of applications that use them (like ReNamer, Mp3Tag and so on).

Offline

#8 2009-01-05 19:23

eR@SeR
Senior Member
From: Земун, Србија
Registered: 2008-01-23
Posts: 353

Re: Examples of Regular Expressions

Expression: ^(\D*?)(\d)                       (removes non digit from start of line, until first digit)
Replace: $2


TRUTH, FREEDOM, JUSTICE and FATHERLAND are the highest morale values which human is born, lives and dies for!

Offline

#9 2009-01-07 22:08

krtek
Senior Member
From: Łódź (Poland)
Registered: 2008-02-21
Posts: 262

Re: Examples of Regular Expressions

eR@SeR wrote:

Expression: ^(\D*?)(\d)                       (removes non digit from start of line, until first digit)
Replace: $2

Hi, eR@SeR!
Long time not hear (from me big_smile)...
Nice one, but the "?" is redundant. ^(\D*)(\d) will do the same job (probably quicker, but I don't think you could feel the difference, maybe at 1000000 files tongue

As I'm a linguist, I'll give you few synonims for your expression. Try to find out what's what. If we care only for the output, they all do exactly the same.

^(\D+)(\d)      -  this one won't do anything, unless it is necessary
^([^\d]+)(\d)  - so will this one
^([^\d]*)(\d)   - this theoretically (depends on how it is implemented in regex) might still try to replace if there is nothing to replace (if the digit is first sign in the filename) - same as yours
^(\D+)(\d+) -  this one is a bit redundant

Edited by krtek:
And one more thing... We can omit first pair of brackets. They're unnecessary. If we write any of expressions above without them, eg. ^\D+(\d), and change Replace part to $1, we will get same output...

Ok, you're right, I must have been bored a bit.
Cheers!

Last edited by krtek (2009-01-11 11:07)


Regular Expressions are not as hard to understand as you may think. Check ReNamer's manual or nice Regular Expressions tutorial for more info and start to use full power of applications that use them (like ReNamer, Mp3Tag and so on).

Offline

#10 2009-01-09 20:35

eR@SeR
Senior Member
From: Земун, Србија
Registered: 2008-01-23
Posts: 353

Re: Examples of Regular Expressions

Hi krtek!

Where you've been? Same answer from you perhaps: I didn't find appropriate thread to answer etc. big_smile
Well, you missed one, but I didn't forget that since august. Here is Removing a simbol at last position. A little of action don't harm big_smile

Nice one, but the "?" is redundant.

...Or that...

Ok, you're right, I must have been bored a bit.

No, no... This is more or less important explanation for users and you should intervent in this and other cases.
This is your "field" and correct/improve any RegEx for which you think that should...
One more question:

(removes non digit from start of line, until first digit)

When we want to limit the action until somewhere we use brackets ("(\d)") and point to the last reference, in this case "$2"?

P.S. Thank you for explanation in this thread wink

Last edited by eR@SeR (2009-01-09 20:38)


TRUTH, FREEDOM, JUSTICE and FATHERLAND are the highest morale values which human is born, lives and dies for!

Offline

Board footer

Powered by FluxBB