![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programmer
Join Date: Oct 2005
Posts: 54
Rep Power: 3
![]() |
PHP & newlines Regular expressions
Hi all!
I have a preformatted text where I want to run a regular expression. The text is something like : [PHP] codeA *->refbrjkgbrkjgbrjukbgreukgbrukgbegurebgrek klgfhnrjkgnrjgbrejkgbrjkbegjkrbbrkegbrejb reopjgporejytopjrotjrphjpojprhjtporhtjrph codeB *->4p3tuop43tu4optj4rofdgfnreklfnrklefnrkenr ropeturojgorejgeolgjrelgjrlgrhelkghelghlg regjrejgoejeojigjreilgjelgjrilgjlerjgelrj [/PHP] From each 'paragraph', I want to match the part under codeA and codeB The problem I have is with matching the newline characters. I write : preg_match('/code.{1}\n\*->(.*)\ncode./') but it doesn't match... I think the problem is the newlines |
|
|
|
|
|
#2 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
What I'm going to say may be wrong. I can't lay my hands immediately on the reference I have in mind. The newline may not be interpreted as such inside single quotes.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#3 |
|
Programmer
Join Date: Oct 2005
Posts: 54
Rep Power: 3
![]() |
DaWei, thanx for the post...
I tried using double quotes, but doesn't do anything... and the weird thing is that, for instance, spaces are matced with \s but why newlines don't???? I can't figure it out..... |
|
|
|
|
|
#4 |
|
Expert Programmer
Join Date: Aug 2005
Location: Rotterdam, the Netherlands
Posts: 942
Rep Power: 4
![]() |
Maybe you have to use \r\n, instead of only \n. Absolutely not sure, though.
|
|
|
|
|
|
#5 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Your particular text, as represented in the post box, does indeed contain both a return and a newline:
Hex/Ascii
20 63 6f 64 65 41 0d 0a 2a 2d 3e 72.... (20 is a space)
c o d e A \r \n * - > r ....
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#6 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
I don't know how you are producing your string, as shown in the code box of your post. If you produce it with the line breaks as actual codes rather than some unpredictable presentation that varies from display device to display device and system to system, I think you'll find success. In other words,
"codeA\r\n*->refbrjkgbrkjgbrjukbgreukgbrukgbegurebgrek\r\nklgfhnrjkgnrjgbrejkgbrjkbegjkrbbrkegbrejb\r\nreopjgporejytopjrotjrphjpojprhjtporhtjrph"
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|