|
||||||
| Perl Discussion for the PERL language - Practical Extraction and Reporting Language, is a programming language often used for creating CGI programs. |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Display Modes |
|
|||
|
Just started using regex expressions but i'm stumped when it comes to the following. I'm parsing data and I want to strip away the following tag found in xml files but leave the text it surrounds intact.
<![CDATA[ text ]]> It's the text inside I would like to keep. I tried searching the net for the proper way but all i've come up with is a way to strip the tag along with what is contained inside it. If I break up my single expression into two and looking it as two regex substitution calls, I get compile time errors...Thanks in advance. what I have now, that strips away the tag and what's inside it... $string =~ s/<![CDATA[]]//i; #substitute it with nothing (strip it away) |
| Sponsored Links |
|
|
|
|||||
|
The way I would approach this is as follows:
There are three sections of your code: "<![CDATA[ ", text, and " ]]>" If you wrap the middle in parenthesis, as (.*), you can return it using $1 (or similar)
__________________
CodeCall Blog | CodeCall Wiki | Shareware | Linux Forum Programming is a branch of mathematics. |
|
|||
|
won't that only work if i'm trying to find a match? I can't do that with the substitution expression can I? What if I have multiple CDATA tags to worry about? I'll try playing around with it but i'm not entirely sure I will be writing this out correctly.
|
|
|||
|
The regexp works.
Code:
$string = '<![CDATA[this is a test]]>'; $string =~ s/<!\[CDATA\[(.*?)\]\]>/$1/gis; print $string; |
|
|||
|
your right, I got it working correctly. Thanks! Can that same code segment for the substitution be used on lets say a string with quotation marks? Cause I would like to just use it again but for a string that has something like
<some element url = "www.gosomewhere.com" type = "some type" /> and use the expression you gave me to grab the url between the first set of parenthesis. Would I have to encode the quotation marks though? $string =~ s/"(.*?)"/$1/is; #wouldn't work like this, would it? |
|
|||
|
nah, doesn't seem to work. I thought putting a couple slashes in '\' would let me include the quotation marks, but that doesn't seem to work either. Should I be using some kind of special characters instead of quotation marks?
|
|
|||
|
unless you want to modify the string there is no need to use a s/// regexp, just use m// to find the pattern and assign it to $1 or to a scalar:
Code:
$string = '<some element url = "www.gosomewhere.com" type = "some type" />'; $string =~ /"(.*?)"/; print $1; Code:
$string = '<some element url = "www.gosomewhere.com" type = "some type" />'; ($domain) = $string =~ /"(.*?)"/; print $domain; |
| Sponsored Links |
|
|
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | Search this Thread |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Tutorial: C# Regex | NeedHelp | CSharp Tutorials | 2 | 05-01-2007 07:44 AM |
| regex (yuck) | John | PHP Forum | 9 | 08-09-2006 01:35 AM |
| Boolean expressions | Sionofdarkness | Java Help | 11 | 08-02-2006 08:25 PM |
| Arithmetic Expressions | Sionofdarkness | Java Help | 2 | 07-29-2006 01:23 PM |
| Regular expressions | Nightracer | General Programming | 6 | 07-24-2006 10:57 PM |
| WingedPanther | ........ | 2753.6 |
| Xav | ........ | 2704 |
| Brandon W | ........ | 1702.32 |
| John | ........ | 1207.73 |
| marwex89 | ........ | 1175.24 |
| morefood2001 | ........ | 966.05 |
| dcs | ........ | 655.75 |
| Steve.L | ........ | 475.59 |
| orjan | ........ | 418.58 |
| Aereshaa | ........ | 383.54 |
Goal: 100,000 Posts
Complete: 98%