remove only fist space in string

A swapping-ground for Regular Expression syntax

remove only fist space in string

Postby larryisme » Sun Nov 10, 2013 11:55 am

Need to remove the spaces around the first "-" in file names. Only around the first hyphen.

have now:

NS008 - 15 - Wiggins, John and Audrey - Has Anybody Seen Amy.zip
NS008 - 06 - Cyrus, Billy Ray - Somebody New.zip

need to be:

NS008-15 - Wiggins, John and Audrey - Has Anybody Seen Amy.zip
NS008-06 - Cyrus, Billy Ray - Somebody New.zip
larryisme
 
Posts: 6
Joined: Sun Nov 10, 2013 11:47 am

Match 1st occurrence of Space-Space

Postby truth » Sun Nov 10, 2013 2:11 pm

1Regex:
(.*?) - (.*)
\1-\2
truth
 
Posts: 221
Joined: Tue Jun 25, 2013 3:39 am
Location: Earth, OrionArm, MilkyWay

Re: remove only fist space in string

Postby larryisme » Sun Nov 10, 2013 10:20 pm

Works excellent! Thank you. If you get bored could you take a moment and break it down what each part does so I can learn what the coding commands are. Don't know anything about the codes and would love to learn it.

Thanks much
larryisme
 
Posts: 6
Joined: Sun Nov 10, 2013 11:47 am

Match 1st occurrence of Space-Space

Postby truth » Wed Nov 13, 2013 1:41 am

No worries,
There is a good primer to regexes @ www.bulkrenameutility.co.uk/forum/viewtopic.php?f=3&t=96
Best way to learn is by trying-out different Match/Replacements in BRU (with Options/AutoSelect-Checked)
It gives you some visual feedback as you change the different parts, just keep them simple to start with.

I'll do by best with this example:
Match: (.*?) - (.*)
Repla: \1-\2

Text-strings can be grouped within ()
When text is grouped, you can represent that text with \1, \2, \3, etc in the ReplaceBox

To represent what text gets matched, you can either type the literal text, or use wildcards
An example: To match abc, you could use abc or ... (the .'s are wildcards, meaning any-3chars whatsoever)
With wildcards, you often want to specify a range of how-many chars to match:
.* match 0-or-more, but as many as possible (without disabling an upcoming match, thats where it'll stop)
.+ match 1-or-more (same as above)
.+? match 1-or-more (but as few chars as possible, until next upcoming match succeeds)

So in this example, your match is: (AsFewCharsAsPossible)Space-Space(AsManyAsPoss)
We didnt group Space-Space within () because that's the part you want to edit in Replace (killing the spaces)
Thus a replacement of:\1-\2

Another way is to match: (AsFewCharsAsPossible)Space(-)Space(AsManyAsPoss) and replace with \1\2\3
(.*?) (-) (.*)
\1\2\3

Notice how the non-grouped text gets discarded, unless typed back into the replacement
Theres a bit more to it, but its not really complicated.. Again, the best way to learn is while in BRU
Just dont forget to at least put \1 into your replace so you see how your matching changes.

A final example, it only matches filenames beginning as CapCap###Space-Space
(so it wont touch any files you may have already renamed)
^([A-Z]{2}[0-9]{3}) (-) (.*)
\1\2\3
truth
 
Posts: 221
Joined: Tue Jun 25, 2013 3:39 am
Location: Earth, OrionArm, MilkyWay

Re: remove only fist space in string

Postby larryisme » Mon Nov 18, 2013 4:18 am

Thanks much! :D
larryisme
 
Posts: 6
Joined: Sun Nov 10, 2013 11:47 am


Return to Regular Expressions