Remove everything until first char is letter or number

A swapping-ground for Regular Expression syntax

Remove everything until first char is letter or number

Postby hiltilg » Tue Nov 09, 2021 7:37 am

Hello,

i try to rename few files witch have the same "system" but not named the same way. Like:

04 - somefile.mp3
02. 1somefile2.mp3
10 - 2somefile3.mp3
05.somefile4.mp3
06-somefile5.mp3
.... and many more

And many combinations of that. And they all have common, that they begin with a number and have space and - as separator or . and space. I try to rename all to:

somefile.mp3
1somefile2.mp3
2somefile3.mp3
somefile4.mp3
somefile5.mp3

i cant work with cut or something, because 04. name are 4 chars and 04 - name are 5 chars.

I tried with regex. but i dont understand it how to do it.

I want have everything removed until the first letter is reached or the first number (after the first two and the separator) is reached.

Cant somebody help me with that?

Thanks. And sry for my english.
hiltilg
 
Posts: 3
Joined: Tue Nov 09, 2021 7:29 am

Re: Remove everything until first char is letter or number

Postby Luuk » Tue Nov 09, 2021 5:02 pm

If they do not all have spaces, can use a "Match" and "Replace" like...
^\d* ?[-.] ?(.+)
\1

If they do all start with 2-numbers, can also change the * ===> {2}
Luuk
 
Posts: 690
Joined: Fri Feb 21, 2020 10:58 pm

Re: Remove everything until first char is letter or number

Postby hiltilg » Wed Nov 10, 2021 3:17 am

That works!

Could you explain me the Syntax of that regex? Because i want to learn that :)

Thank you very much.
hiltilg
 
Posts: 3
Joined: Tue Nov 09, 2021 7:29 am

Re: Remove everything until first char is letter or number

Postby Luuk » Wed Nov 10, 2021 10:31 pm

The answer is actually a mistake, because the "Match" should be... ^\d+ ?[-.] ?(.+)
These red parts is called quantifiers, and they do say how much of something to match.
* ===== 0-or-more
? ===== 0-or-one
+ ===== 1-or-more
{2} ==== Exactly 2
{3,} === 3-or-more
{2,5} == 2-thru-5

\d ===== Any number/digit
[-.] ==== Everything inside of [here] is just a list of characters that can be matched.
======= But '-' must be first, because if its anywhere else, then it can only say a 'range' of characters.
======= Like [a-z] says any lowercase english letter, so if also wanting '-' then must say it like [-a-z].


So with... ^\d+ ?[-.] ?(.+)

^ ======= The very beginning.
\d+ ===== 1-or-more digits.
Space? == 0-or-one space.
[-.] ===== 1 '-' or '.' (only 1 because no quantifier, but [-.]+ could match '-' or even '.-.-----........').
Space? == 0-or-one space.
(.+) ===== 1-or-more any-character. The '.' always says any-character, except when its inside of [here].
========= Since .+ is inside of the round brackets, then replace can use \1 for this matched text.

The first answer is not really good, because names like...
.12345.txt ===> 12345.txt
- Name.txt ===> Name.txt

Its because \d* can match 0-digits at the beginning, but \d+ only matches 1-or-more digits at the beginning.
If all the files did begin with only having two or three digits, a better Match would be... ^\d{2,3} ?[-.] ?(.+)
So then if having any names like 2021-somefile or 2021.somefile, they will not have the years removed.
Luuk
 
Posts: 690
Joined: Fri Feb 21, 2020 10:58 pm

Re: Remove everything until first char is letter or number

Postby hiltilg » Thu Nov 11, 2021 12:11 am

Thank you very much! :)
hiltilg
 
Posts: 3
Joined: Tue Nov 09, 2021 7:29 am


Return to Regular Expressions


cron