PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Sun Dec 08, 2019 8:28 am

All times are UTC - 5 hours




Post new topic Reply to topic  [ 15 posts ] 
Author Message
PostPosted: Tue Dec 13, 2011 4:21 pm 
Offline
Forum Newbie

Joined: Tue Dec 13, 2011 3:58 pm
Posts: 2
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


Top
 Profile  
 
PostPosted: Tue Dec 13, 2011 4:37 pm 
Offline
Forum Regular
User avatar

Joined: Tue Sep 28, 2010 11:41 am
Posts: 984
Location: Columbus, Ohio
Notepad or Notepad++?

For Notepad++, with Regular Expressions checked,
FIND: 12([\.,])50
REPLACE: 12\180


Last edited by twinedev on Tue Dec 13, 2011 4:44 pm, edited 1 time in total.

Top
 Profile  
 
PostPosted: Tue Dec 13, 2011 4:39 pm 
Offline
Forum Newbie

Joined: Tue Dec 13, 2011 3:58 pm
Posts: 2
Notepad++ (sorry for the mistake)


Top
 Profile  
 
PostPosted: Thu Dec 15, 2011 2:59 pm 
Offline
Forum Commoner
User avatar

Joined: Thu Dec 15, 2011 2:40 pm
Posts: 85
Location: Nelson, NZ
Extending the answer for other editors:
EditPadPro:

FIND: 12([,.])80
REPLACE: 12\150

(No need to escape the dot.)


Top
 Profile  
 
PostPosted: Thu Dec 15, 2011 5:13 pm 
Offline
Forum Regular
User avatar

Joined: Tue Sep 28, 2010 11:41 am
Posts: 984
Location: Columbus, Ohio
Is the dot matching a period, or matching any character?


Top
 Profile  
 
PostPosted: Thu Dec 15, 2011 5:39 pm 
Offline
Forum Commoner
User avatar

Joined: Thu Dec 15, 2011 2:40 pm
Posts: 85
Location: Nelson, NZ
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.)


Top
 Profile  
 
PostPosted: Fri Dec 16, 2011 12:40 am 
Offline
Forum Regular
User avatar

Joined: Tue Sep 28, 2010 11:41 am
Posts: 984
Location: Columbus, Ohio
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


Top
 Profile  
 
PostPosted: Fri Dec 16, 2011 12:57 am 
Offline
Forum Commoner
User avatar

Joined: Thu Dec 15, 2011 2:40 pm
Posts: 85
Location: Nelson, NZ
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


Top
 Profile  
 
PostPosted: Sat Dec 17, 2011 11:21 pm 
Offline
Forum Regular
User avatar

Joined: Tue Sep 28, 2010 11:41 am
Posts: 984
Location: Columbus, Ohio
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


Top
 Profile  
 
PostPosted: Sun Dec 18, 2011 12:02 am 
Offline
Forum Commoner
User avatar

Joined: Thu Dec 15, 2011 2:40 pm
Posts: 85
Location: Nelson, NZ
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


Top
 Profile  
 
PostPosted: Mon Dec 19, 2011 4:59 pm 
Offline
Forum Regular
User avatar

Joined: Tue Sep 28, 2010 11:41 am
Posts: 984
Location: Columbus, Ohio
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


Top
 Profile  
 
PostPosted: Mon Dec 19, 2011 5:09 pm 
Offline
Forum Commoner
User avatar

Joined: Thu Dec 15, 2011 2:40 pm
Posts: 85
Location: Nelson, NZ


Top
 Profile  
 
PostPosted: Mon Dec 19, 2011 11:50 pm 
Offline
Forum Regular
User avatar

Joined: Tue Sep 28, 2010 11:41 am
Posts: 984
Location: Columbus, Ohio
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


Top
 Profile  
 
PostPosted: Tue Dec 20, 2011 12:05 am 
Offline
Forum Commoner
User avatar

Joined: Thu Dec 15, 2011 2:40 pm
Posts: 85
Location: Nelson, NZ
Just in case: I didn't mean to give you a hard time, just being playful. ;)
Wishing you a fun day,
-A


Top
 Profile  
 
PostPosted: Tue Dec 20, 2011 5:39 pm 
Offline
Forum Regular
User avatar

Joined: Tue Sep 28, 2010 11:41 am
Posts: 984
Location: Columbus, Ohio
Don't worry, didn't take it negative at all. Have a great day too!

-Greg


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 15 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group