Regex Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming Languages - MoreRegex Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old July 25th, 2012, 05:01 PM
pedro1977 pedro1977 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2012
Posts: 5 pedro1977 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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

Reply With Quote
  #2  
Old July 25th, 2012, 05:28 PM
Jacques1's Avatar
Jacques1 Jacques1 is offline
pollyanna
Click here for more information.
 
Join Date: Jul 2012
Location: Germany
Posts: 1,864 Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 1 Day 20 h 35 m 35 sec
Reputation Power: 813
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.

Reply With Quote
  #3  
Old July 25th, 2012, 05:44 PM
pedro1977 pedro1977 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2012
Posts: 5 pedro1977 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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

Reply With Quote
  #4  
Old July 25th, 2012, 11:20 PM
abareplace abareplace is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2011
Posts: 29 abareplace User rank is First Lieutenant (10000 - 20000 Reputation Level)abareplace User rank is First Lieutenant (10000 - 20000 Reputation Level)abareplace User rank is First Lieutenant (10000 - 20000 Reputation Level)abareplace User rank is First Lieutenant (10000 - 20000 Reputation Level)abareplace User rank is First Lieutenant (10000 - 20000 Reputation Level)abareplace User rank is First Lieutenant (10000 - 20000 Reputation Level)abareplace User rank is First Lieutenant (10000 - 20000 Reputation Level)abareplace User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 8 h 25 m 9 sec
Reputation 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?

Reply With Quote
  #5  
Old July 27th, 2012, 04:56 PM
pedro1977 pedro1977 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2012
Posts: 5 pedro1977 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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)

Reply With Quote
  #6  
Old July 29th, 2012, 07:34 PM
abareplace abareplace is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2011
Posts: 29 abareplace User rank is First Lieutenant (10000 - 20000 Reputation Level)abareplace User rank is First Lieutenant (10000 - 20000 Reputation Level)abareplace User rank is First Lieutenant (10000 - 20000 Reputation Level)abareplace User rank is First Lieutenant (10000 - 20000 Reputation Level)abareplace User rank is First Lieutenant (10000 - 20000 Reputation Level)abareplace User rank is First Lieutenant (10000 - 20000 Reputation Level)abareplace User rank is First Lieutenant (10000 - 20000 Reputation Level)abareplace User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 8 h 25 m 9 sec
Reputation Power: 0
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)


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.


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!

Reply With Quote
  #7  
Old July 30th, 2012, 04:49 PM
pedro1977 pedro1977 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2012
Posts: 5 pedro1977 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 7 m 12 sec
Reputation Power: 0
Quote:
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??

Reply With Quote
  #8  
Old July 30th, 2012, 08:37 PM
abareplace abareplace is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2011
Posts: 29 abareplace User rank is First Lieutenant (10000 - 20000 Reputation Level)abareplace User rank is First Lieutenant (10000 - 20000 Reputation Level)abareplace User rank is First Lieutenant (10000 - 20000 Reputation Level)abareplace User rank is First Lieutenant (10000 - 20000 Reputation Level)abareplace User rank is First Lieutenant (10000 - 20000 Reputation Level)abareplace User rank is First Lieutenant (10000 - 20000 Reputation Level)abareplace User rank is First Lieutenant (10000 - 20000 Reputation Level)abareplace User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 8 h 25 m 9 sec
Reputation Power: 0
Does the following work for you?

Code:
Search for:

(\S)-(\S)

Replace with:

\1 \2

Reply With Quote
  #9  
Old August 1st, 2012, 09:20 PM
pedro1977 pedro1977 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2012
Posts: 5 pedro1977 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreRegex Programming > Finding files with more than one of the same symbol.

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap