#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2012
    Posts
    5
    Rep 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
  2. #2
  3. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    Hi,

    What's the programming language?

    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 "-".



    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.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2012
    Posts
    5
    Rep Power
    0
    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
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2011
    Posts
    29
    Rep Power
    0
    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?
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2012
    Posts
    5
    Rep Power
    0
    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)
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2011
    Posts
    29
    Rep Power
    0
    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)
    Code:
    -(.*?)\s*-\s*
    .*? 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.


    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!
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2012
    Posts
    5
    Rep Power
    0
    Originally Posted by abareplace
    Code:
    -(.*?)\s*-\s*
    .*? 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??
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2011
    Posts
    29
    Rep Power
    0
    Does the following work for you?

    Code:
    Search for:
    
    (\S)-(\S)
    
    Replace with:
    
    \1 \2
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2012
    Posts
    5
    Rep Power
    0
    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

IMN logo majestic logo threadwatch logo seochat tools logo