The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages - More
> Regex Programming
|
Finding files with more than one of the same symbol.
Discuss Finding files with more than one of the same symbol. in the Regex Programming forum on Dev Shed. Finding files with more than one of the same symbol. Regular expressions forum covering PCRE and POSIX techniques, practices, and standards. Regular expressions help shorten coding time by providing the ability to compact many lines of code into one string.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

July 25th, 2012, 05:01 PM
|
|
Registered User
|
|
Join Date: Jul 2012
Posts: 5
Time spent in forums: 1 h 7 m 12 sec
Reputation Power: 0
|
|
|
Finding files with more than one of the same symbol.
I would like to write a regex statmentment that finds the following.
For search statement
Any filename with more than one - in it.
For the replace statement I want it to replace all - with a space except the second which should be kept, if there are no spaces on either side of this second one (there are symbols numbers etc) i would like spaces added (dont want to replace what ever symbol number is on either side - simply creating a padding).
Any ideas would be helpful, ive been banging my head against the wall over this one grrrrrr.
Peter
|

July 25th, 2012, 05:28 PM
|
 |
pollyanna
|
|
Join Date: Jul 2012
Location: Germany
|
|
Hi,
What's the programming language?
Quote: | Originally Posted by pedro1977 I would like to write a regex statmentment that finds the following.
For search statement
Any filename with more than one - in it. |
That would be a "-" followed by any number of other characters followed by another "-".
/-[^-]*-/
But I'm not sure a regex is really a good approach. It might make more sense to simply count the "-".
Quote: | Originally Posted by pedro1977 For the replace statement I want it to replace all - with a space except the second which should be kept, if there are no spaces on either side of this second one (there are symbols numbers etc) i would like spaces added (dont want to replace what ever symbol number is on either side - simply creating a padding). |
Don't use a regex for this, it will get too complicated. Simply loop over the "-" with a counter.
|

July 25th, 2012, 05:44 PM
|
|
Registered User
|
|
Join Date: Jul 2012
Posts: 5
Time spent in forums: 1 h 7 m 12 sec
Reputation Power: 0
|
|
Quote: | Originally Posted by Jacques1 Hi,
What's the programming language?
That would be a "-" followed by any number of other characters followed by another "-".
/-[^-]*-/
But I'm not sure a regex is really a good approach. It might make more sense to simply count the "-".
Don't use a regex for this, it will get too complicated. Simply loop over the "-" with a counter. |
The program is Siren by Scarabée Software (cant post the link)
it is a batch file renamer thats uses regex and maybe others
That would be a "-" followed by any number of other characters followed by another "-".
Yes there are not two -- together in any file name, they are separated but they can either have spaces between or around them or other letters/ numbers.
Peter
eg. john-edmonds-1carnival-movies.doc
should be john edmonds - 1carnival movies.doc
|

July 25th, 2012, 11:20 PM
|
|
|
Hi, Peter,
please do the replacement in two steps:
Step 1.
Code:
Search for:
-(.*?)\s*-\s*
Replace to:
\1 -
(space, \1, space, -, space)
Step 2.
Replace all remaining hyphens (-) with spaces.
Does this help you?
|

July 27th, 2012, 04:56 PM
|
|
Registered User
|
|
Join Date: Jul 2012
Posts: 5
Time spent in forums: 1 h 7 m 12 sec
Reputation Power: 0
|
|
Quote: | Originally Posted by abareplace Hi, Peter,
please do the replacement in two steps:
Step 1.
Code:
Search for:
-(.*?)\s*-\s*
Replace to:
\1 -
(space, \1, space, -, space)
Step 2.
Replace all remaining hyphens (-) with spaces.
Does this help you? |
wow it works, it finds the correct entries, replaces the first one with a space, the second is kept and if there is anything either side, it adds a buffer space in otherwise it doesnt touch it. The only problem is it doesnt replace any of the hyphens with a space if they occur after the second one. Id like to do it all in one go as i dont know how to apply another change to the same search list. Otherwise you could add another search i guess searching for more than one again and then replace the second, third fourth ones with space etc.??????
I did not enter this and dont understand where im suppose to put it?????
(space, \1, space, -, space)
Step 2?????? i have to manually do this?
If you could explain some of those symbols in their entirity it would be helpful, what a bizarre but interesting language o)
|

July 29th, 2012, 07:34 PM
|
|
|
Quote: | Originally Posted by pedro1977 If you could explain some of those symbols in their entirity it would be helpful, what a bizarre but interesting language o) |
.*? is anything (any character repeated zero or more times). Similarly, \s* is nothing or several space characters. Question mark in .*? makes it non-greedy, so we find the next hyphen, not the last one.
Generally, the idea is: find the first hyphen, then anything after it up to the next hyphen, which can be surrounded with optional space characters.
Parentheses around (.*?) capture the word between hyphens (in john-edmonds-1carnival-movies.doc, edmonds will be captured). In the replacement, we can refer to the word as \1.
If you want to find out more, welcome to my site.
Quote: | Originally Posted by pedro1977 The only problem is it doesnt replace any of the hyphens with a space if they occur after the second one... Step 2?????? i have to manually do this? |
Yes, do the second search on the same set of files and replace a hyphen with space:
Code:
Search for: -
Replace with: (put a space here)
Unfortunately, I'm not familar with Siren, but it should replace all remaining hyphens with spaces.
Good luck!
|

July 30th, 2012, 04:49 PM
|
|
Registered User
|
|
Join Date: Jul 2012
Posts: 5
Time spent in forums: 1 h 7 m 12 sec
Reputation Power: 0
|
|
Quote: | Originally Posted by abareplace
.*? is anything (any character repeated zero or more times). Similarly, \s* is nothing or several space characters. Question mark in .*? makes it non-greedy, so we find the next hyphen, not the last one.
Generally, the idea is: find the first hyphen, then anything after it up to the next hyphen, which can be surrounded with optional space characters.
Parentheses around (.*?) capture the word between hyphens (in john-edmonds-1carnival-movies.doc, edmonds will be captured). In the replacement, we can refer to the word as \1.
If you want to find out more, welcome to my site.
Yes, do the second search on the same set of files and replace a hyphen with space:
Code:
Search for: -
Replace with: (put a space here)
Unfortunately, I'm not familar with Siren, but it should replace all remaining hyphens with spaces.
Good luck! |
Thanks for the link, ive read it, problem with the the search is I believe it would replace all the remaining hyphens which I do not wish to do. I would like to keep the first one in the text but replace all the remaining ones with a space. I looked through your site (good site btw) but couldnt find anything. i thought maybe
(-){2,????} but i want the ???? to be unlimited (until the last one found) and two would replace the second one in the list first??
|

July 30th, 2012, 08:37 PM
|
|
|
Does the following work for you?
Code:
Search for:
(\S)-(\S)
Replace with:
\1 \2
|

August 1st, 2012, 09:20 PM
|
|
Registered User
|
|
Join Date: Jul 2012
Posts: 5
Time spent in forums: 1 h 7 m 12 sec
Reputation Power: 0
|
|
Quote: | Originally Posted by abareplace Does the following work for you?
Code:
Search for:
(\S)-(\S)
Replace with:
\1 \2
|
Yes that worked fine, thanks very much for your help especially for following this all the way through. As a recap for others with the same problem, the solution was as follows.
Step 1.
Search for:
-(.*?)\s*-\s*
Replace to:
\1 -
Step 2.
Search for:
(\S)-(\S)
Replace with:
\1 \2
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|