NuSphere Forums Forum Index
NuSphere Forums
Reply to topic
regexpression replace
Veteran

Joined: 31 Jul 2003
Posts: 152
Location: Fargo, ND
Reply with quote
I'm having a tough time using the reg expression replace functionality. My code was coming from windows and is now on a linux box. Apparently phpEd changes \r\n to \n\n. So now i have 100 some files all double spaced. I could do a replace if i could figure out the expression syntax for finding \n\n and replaceing with \n. No expressions that i've used before are working. Anyone have a clue ?!
View user's profileFind all posts by deltenerSend private messageVisit poster's website
Site Admin

Joined: 13 Jul 2003
Posts: 8334
Reply with quote
Fortunately, phpED has NO ability to make such stupid troubles like doubling line breaks. It is possibly done by your FTP server that misinterprets \r or so.
PHPED does not deal with \n nor \r during editing the file and does not store them in the editing buffer.

If your original file (before opening in phpED) is Windows-like (\r\n) PHPED by default will save it as \r\n. If it is Unix-like (\n) PHPEd will save as \n and finally if file is Mac-like (\r) PHPEd saves as \r.
If you want to change file type you have to open SaveAs dialog, select proper output type and press OK.
View user's profileFind all posts by dmitriSend private messageVisit poster's website
regexp replace


Joined: 17 May 2005
Posts: 12
Reply with quote
i have similar question regarding how regexp replaces work in phped..

I have an expression looking for url ids within a certain context
Code:
id=.+'><b>

and i´d like to do a
Code:
$1somestring

that´d result in id=13'><b>somestring

for me the search works but the replace doesn´t, it seems phped reads the replacement expression literally?

- oliver
View user's profileFind all posts by oliverazevedobarnesSend private message
Site Admin

Joined: 13 Jul 2003
Posts: 8334
Reply with quote
\1, \2 \3 etc stand for marking places where the corresponding subpatterns would be inserted to.

For example if you enter
find:
id=(.)+'><b>
replace to:
\1something

whatsoever is found on the "." will be inserted before "something":
In other words id=13'><b> will be replaced with 13something

If you need \1 to be inserted as \1, you have to escape it.
As of $1, it is treated literally anyway.
View user's profileFind all posts by dmitriSend private messageVisit poster's website
\1 gets treated literally for me


Joined: 17 May 2005
Posts: 12
Reply with quote
hi dmitrie, i did try \1 also, but it just replaces the pattern for \1something...
am I missing something?
View user's profileFind all posts by oliverazevedobarnesSend private message


Joined: 20 May 2004
Posts: 81
Reply with quote
1. If you'd like to replace "id=13><b>" with "id=13><b>something", I recommend you use this search pattern: "id=(\d+)><b>" and this replace pattern: "\0something". \0 will be substituted with full matched string, but \1 will equal to "13", as it's the first subpattern.

2. In order for subpatterns to be replaced, you need to use parenthesis like this: ": ((\d+) - (\d+))" and this is the source string: "hello: 12 - 13". As result of match, \0 = ": 12 - 13", \1 = "12 - 13", \2 = "12", \3 = "13".

Hope that helps.
View user's profileFind all posts by mblshaSend private message


Joined: 17 May 2005
Posts: 12
Reply with quote
dmitrie, thanks for the explanation... I see better now how it should work.
but what I meant was that \1 is not getting replaced. the replace just shows \1 literally, and not the matched pattern. I tried using \0 for the whole pattern, same thing happens.
View user's profileFind all posts by oliverazevedobarnesSend private message
just to be clear


Joined: 17 May 2005
Posts: 12
Reply with quote
the problem isn´t solved yet... I´m still not able to use regex replaces. I understand how it should work, but it´s just not working for me.

thks
oliver
View user's profileFind all posts by oliverazevedobarnesSend private message


Joined: 20 May 2004
Posts: 81
Reply with quote
Sorry, but PhpED 3.3 doesn't support text replacing using regular expression syntax. The matched text will be replaced by the whole 'Replace with' string. And supported RegExp syntax in PhpED 3.3 is very limited too.

Improved RegExp support is one of PhpED 4.0 major features.
View user's profileFind all posts by mblshaSend private message
?


Joined: 17 May 2005
Posts: 12
Reply with quote
mbisha,
i see, I'll be looking forward to 4.0 then.

but what about the funcionality you mentioned?

Quote:
1. If you'd like to replace "id=13><b>" with "id=13><b>something", I recommend you use this search pattern: "id=(\d+)><b>" and this replace pattern: "\0something". \0 will be substituted with full matched string, but \1 will equal to "13", as it's the first subpattern.

2. In order for subpatterns to be replaced, you need to use parenthesis like this: ": ((\d+) - (\d+))" and this is the source string: "hello: 12 - 13". As result of match, \0 = ": 12 - 13", \1 = "12 - 13", \2 = "12", \3 = "13".


this would solve some of my needs right now. do you mean this actually doesn't work at all?

- Oliver
View user's profileFind all posts by oliverazevedobarnesSend private message


Joined: 20 May 2004
Posts: 81
Reply with quote
Replacing using subpatterns is definetely not working, and syntax for patterns used in search is limited too. I don't know what is supported in 3.3, but 4.0 will feature perl-compatible regular expressions.

If you need it now, you may try to write a simple perl script, or use sed (it's available in cygwin).
View user's profileFind all posts by mblshaSend private message


Joined: 17 May 2005
Posts: 12
Reply with quote
thanks mbisha. I assume you were referring to general regexp syntax on your original explanation then...

I don't know perl and have never used sed... but I'm using grep on bbedit, which is pretty good, and using php regexp scripts. just would be nice to do that on the search itself, and since it will in 4.0, i'm happy Smile

by the way, will 4.0 be able to do multiple line search/replace? guess I should check on other posts on the forum...

thanks
oliver
View user's profileFind all posts by oliverazevedobarnesSend private message
regexpression replace
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 vote in polls in this forum
All times are GMT - 5 Hours  
Page 1 of 2  

  
  
 Reply to topic