How to search and replace
Moderator: General Moderators
How to search and replace
Hi,
I would like to find the appropriate regex to search and replace in a source code of an html file :
- search simultaneously 12,50 and 12.50
- and replace it simultaneously respectively by (for instance) 12,80 and 12.80
Search and replace would be done with Notepad, for instance.
Thank you in advance for any help.
Patrick
I would like to find the appropriate regex to search and replace in a source code of an html file :
- search simultaneously 12,50 and 12.50
- and replace it simultaneously respectively by (for instance) 12,80 and 12.80
Search and replace would be done with Notepad, for instance.
Thank you in advance for any help.
Patrick
Re: How to search and replace
Notepad or Notepad++?
For Notepad++, with Regular Expressions checked,
FIND: 12([\.,])50
REPLACE: 12\180
For Notepad++, with Regular Expressions checked,
FIND: 12([\.,])50
REPLACE: 12\180
Last edited by twinedev on Tue Dec 13, 2011 3:44 pm, edited 1 time in total.
Re: How to search and replace
Notepad++ (sorry for the mistake)
Re: How to search and replace
Extending the answer for other editors:
EditPadPro:
FIND: 12([,.])80
REPLACE: 12\150
(No need to escape the dot.)
EditPadPro:
FIND: 12([,.])80
REPLACE: 12\150
(No need to escape the dot.)
Re: How to search and replace
Is the dot matching a period, or matching any character?
Re: How to search and replace
It's matching the dot in 12.50, isn't it?
(It seems the OP has numbers in either European or US-style format, i.e. with a comma or a dot for the decimal point.)
(It seems the OP has numbers in either European or US-style format, i.e. with a comma or a dot for the decimal point.)
Re: How to search and replace
My point was that in many of the Regex engines I have worked with, a period matched ANY character. Actually I just check in RegEx Buddy (who make EditPad Pro, and in all the engines it will build for, a period always matches a single character (some engines do or do not also match newlines)
As an example will 12[.,]50 match 12-50 on that program or 12750
-Greg
As an example will 12[.,]50 match 12-50 on that program or 12750
-Greg
Re: How to search and replace
Err... Sorry, Greg, I don't know how to say this more gently, but I don't think that's right.
Inside the brackets of a character class, i.e. "[]", in all the regex flavors I'm familiar with, a period is a period. You don't need to escape it because it's just a period.
To see that, in RegexBuddy, go to the Test tab. For the expression, paste 12[.]50
Now in the subject box, paste 12-50 12,50 12a50 12550 12.50
You will see that only the 12.50 matches. You can choose Perl, PCRE, Java, anything you like, 12-50 still won't match.
That's a frequent misunderstanding about character classes. There's actually very little that you need to escape inside a character class. If I recall, only the backslash. That's because you can get away with matching the closing bracket ], the dash -, the caret ^ just before the closing bracket. Maybe there is another character that needs to be escaped, I don't recall.
I hope I understood the nature of your reply. If I didn't, sorry.

Wishing you a fun day,
-Andy
Inside the brackets of a character class, i.e. "[]", in all the regex flavors I'm familiar with, a period is a period. You don't need to escape it because it's just a period.
To see that, in RegexBuddy, go to the Test tab. For the expression, paste 12[.]50
Now in the subject box, paste 12-50 12,50 12a50 12550 12.50
You will see that only the 12.50 matches. You can choose Perl, PCRE, Java, anything you like, 12-50 still won't match.
That's a frequent misunderstanding about character classes. There's actually very little that you need to escape inside a character class. If I recall, only the backslash. That's because you can get away with matching the closing bracket ], the dash -, the caret ^ just before the closing bracket. Maybe there is another character that needs to be escaped, I don't recall.
I hope I understood the nature of your reply. If I didn't, sorry.
Wishing you a fun day,
-Andy
Re: How to search and replace
Hmm good to know, I always escaped it even in a class. Learn something new each day LOL Thanks for the info.
RegexBuddy is more than a buddy when it come to regular expressions for me. Learned so much, and use it for so much for anything complicated.
-Greg
RegexBuddy is more than a buddy when it come to regular expressions for me. Learned so much, and use it for so much for anything complicated.
-Greg
Re: How to search and replace
Yes, what an amazing program. I love it too. It's also helped me learn heaps, and still does.
If there's one thing I'd like to see improve, it would be the debug window. I'd love to see a "trace" of the match process with the expression and the subject string side by side. But it's already great to have the feature.
Nice to be in touch, wishing you a fun weekend.
-Andy
If there's one thing I'd like to see improve, it would be the debug window. I'd love to see a "trace" of the match process with the expression and the subject string side by side. But it's already great to have the feature.
Nice to be in touch, wishing you a fun weekend.
-Andy
Re: How to search and replace
I've never used the debug on it. I do love being able to do live tests on actual data while typing the regex. It allows me to do one preg_match to extract many chunks of data on what used to take several find sections.
I once wrote a site scraper years ago, hand coded the entire thing, regex would have made it SOOOOO much easier LOL
I once wrote a site scraper years ago, hand coded the entire thing, regex would have made it SOOOOO much easier LOL
Re: How to search and replace
Wait, isn't that supposed to be bad?I once wrote a site scraper years ago
I'm sure it was for scientific purposes, though.
Wishing you a fun day
Re: How to search and replace
Yes, in hind site, it was not the best choise, but was at the time a grey area as I was picky, read the sites TOS, and there was nothing in there against that, mainly that you were using the info for your own use (which it was for the company I worked for to use to contact potential clients). Also at least I was polite, I manually mined the site by hand (clicking links, do SAVE AS, click next page, SAVE AS...), and then doubled the time it took me to do it and used that for delays so that I was not overloading their system.
Obviously they never detected it and/or didn't care as they never put in simple things that would have made it harder.
-Greg
Obviously they never detected it and/or didn't care as they never put in simple things that would have made it harder.
-Greg
Re: How to search and replace
Just in case: I didn't mean to give you a hard time, just being playful. 
Wishing you a fun day,
-A
Wishing you a fun day,
-A
Re: How to search and replace
Don't worry, didn't take it negative at all. Have a great day too!
-Greg
-Greg