IF in a Regex expresion? (replace something in file names ONLY if file type is xxx)
up vote
-1
down vote
favorite
Ok, I know some regex but this fooling me...
I usually manage every month hundreds of files submited and have to make some checks and replaces before making them available again in our intranet...
I'm doing it locally on hd on Windows, through a file renamer program which can do pcre but only do one line at a time, so all should be in the same regex.
The problem is that I would like to do replacements only if file type is xxx.
For example, replace all spaces for underscores ONLY if extension is jpg|jpeg|jpe
so
this is a test.jpg => this_is_a_test.jpg
this is a another test.jpe => this_is_a_another_test.jpe
this is a test.docx => this is a test.docx
Jpg is an EXAMPLE, I do diferent replaces for each extension and not for all extensions, so something which replaces spaces in the above example in the .docx will be wrong...
is it posible???
regex pcre
add a comment |
up vote
-1
down vote
favorite
Ok, I know some regex but this fooling me...
I usually manage every month hundreds of files submited and have to make some checks and replaces before making them available again in our intranet...
I'm doing it locally on hd on Windows, through a file renamer program which can do pcre but only do one line at a time, so all should be in the same regex.
The problem is that I would like to do replacements only if file type is xxx.
For example, replace all spaces for underscores ONLY if extension is jpg|jpeg|jpe
so
this is a test.jpg => this_is_a_test.jpg
this is a another test.jpe => this_is_a_another_test.jpe
this is a test.docx => this is a test.docx
Jpg is an EXAMPLE, I do diferent replaces for each extension and not for all extensions, so something which replaces spaces in the above example in the .docx will be wrong...
is it posible???
regex pcre
add a comment |
up vote
-1
down vote
favorite
up vote
-1
down vote
favorite
Ok, I know some regex but this fooling me...
I usually manage every month hundreds of files submited and have to make some checks and replaces before making them available again in our intranet...
I'm doing it locally on hd on Windows, through a file renamer program which can do pcre but only do one line at a time, so all should be in the same regex.
The problem is that I would like to do replacements only if file type is xxx.
For example, replace all spaces for underscores ONLY if extension is jpg|jpeg|jpe
so
this is a test.jpg => this_is_a_test.jpg
this is a another test.jpe => this_is_a_another_test.jpe
this is a test.docx => this is a test.docx
Jpg is an EXAMPLE, I do diferent replaces for each extension and not for all extensions, so something which replaces spaces in the above example in the .docx will be wrong...
is it posible???
regex pcre
Ok, I know some regex but this fooling me...
I usually manage every month hundreds of files submited and have to make some checks and replaces before making them available again in our intranet...
I'm doing it locally on hd on Windows, through a file renamer program which can do pcre but only do one line at a time, so all should be in the same regex.
The problem is that I would like to do replacements only if file type is xxx.
For example, replace all spaces for underscores ONLY if extension is jpg|jpeg|jpe
so
this is a test.jpg => this_is_a_test.jpg
this is a another test.jpe => this_is_a_another_test.jpe
this is a test.docx => this is a test.docx
Jpg is an EXAMPLE, I do diferent replaces for each extension and not for all extensions, so something which replaces spaces in the above example in the .docx will be wrong...
is it posible???
regex pcre
regex pcre
asked yesterday
user3393366
384
384
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
up vote
2
down vote
accepted
You need to find spaces, and then look ahead to see the extension:
(?=.*.jp(?:g|e)$)
Note the leading space.
Try it here: https://regex101.com/r/ZgDv7S/1
YES, IS THAT!!!
– user3393366
yesterday
add a comment |
up vote
0
down vote
You said you were on Windows, but do you have WSL (Bash for Linux) or something similar available? If so, here is a quick way to do this from the Linux command line:
rename 's/ /_/g;' *.jpg *.jpe
Explanation: rename
runs the given Perl script on the name of the specified files.
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
You need to find spaces, and then look ahead to see the extension:
(?=.*.jp(?:g|e)$)
Note the leading space.
Try it here: https://regex101.com/r/ZgDv7S/1
YES, IS THAT!!!
– user3393366
yesterday
add a comment |
up vote
2
down vote
accepted
You need to find spaces, and then look ahead to see the extension:
(?=.*.jp(?:g|e)$)
Note the leading space.
Try it here: https://regex101.com/r/ZgDv7S/1
YES, IS THAT!!!
– user3393366
yesterday
add a comment |
up vote
2
down vote
accepted
up vote
2
down vote
accepted
You need to find spaces, and then look ahead to see the extension:
(?=.*.jp(?:g|e)$)
Note the leading space.
Try it here: https://regex101.com/r/ZgDv7S/1
You need to find spaces, and then look ahead to see the extension:
(?=.*.jp(?:g|e)$)
Note the leading space.
Try it here: https://regex101.com/r/ZgDv7S/1
answered yesterday
Sweeper
59.9k967134
59.9k967134
YES, IS THAT!!!
– user3393366
yesterday
add a comment |
YES, IS THAT!!!
– user3393366
yesterday
YES, IS THAT!!!
– user3393366
yesterday
YES, IS THAT!!!
– user3393366
yesterday
add a comment |
up vote
0
down vote
You said you were on Windows, but do you have WSL (Bash for Linux) or something similar available? If so, here is a quick way to do this from the Linux command line:
rename 's/ /_/g;' *.jpg *.jpe
Explanation: rename
runs the given Perl script on the name of the specified files.
add a comment |
up vote
0
down vote
You said you were on Windows, but do you have WSL (Bash for Linux) or something similar available? If so, here is a quick way to do this from the Linux command line:
rename 's/ /_/g;' *.jpg *.jpe
Explanation: rename
runs the given Perl script on the name of the specified files.
add a comment |
up vote
0
down vote
up vote
0
down vote
You said you were on Windows, but do you have WSL (Bash for Linux) or something similar available? If so, here is a quick way to do this from the Linux command line:
rename 's/ /_/g;' *.jpg *.jpe
Explanation: rename
runs the given Perl script on the name of the specified files.
You said you were on Windows, but do you have WSL (Bash for Linux) or something similar available? If so, here is a quick way to do this from the Linux command line:
rename 's/ /_/g;' *.jpg *.jpe
Explanation: rename
runs the given Perl script on the name of the specified files.
answered yesterday
bitinerant
343
343
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53238561%2fif-in-a-regex-expresion-replace-something-in-file-names-only-if-file-type-is-x%23new-answer', 'question_page');
}
);
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password