954,523 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Get data between tag problem

Hi

I have problem to get data from tag (eg: ).

For example the string is like this:

$string = "<TEXT>

Some text

</TEXT>";

if($string =~ /<(TEXT)>\s*(.*)\s*<\/\1>/g)
{
	$result = $2;
	print $result, "\n";
}


It works fine, but if the string is like this:

$string = "<TEXT>
Some text
Another text
</TEXT>";


I got nothing.

Thanks in advance.

back2arie
Newbie Poster
2 posts since Jan 2010
Reputation Points: 10
Solved Threads: 0
 

things can be that easy.

$string = "<TEXT>
Some text
Another text
</TEXT>";

@s=split /<\/TEXT>/, $string;
$s[0] =~ s/.*<TEXT>// ;
print $s[0];
ghostdog74
Junior Poster
156 posts since Apr 2006
Reputation Points: 75
Solved Threads: 44
 

Thanks, it's works :)

back2arie
Newbie Poster
2 posts since Jan 2010
Reputation Points: 10
Solved Threads: 0
 

Hi back2arie,

For what you wrote the regular expression that's all most right.

if($string =~ /<(TEXT)>\s*(.*)\s*<\/\1>/g)


But $string had the text in multiple line. So it's not worked. You will be add 's' modifier in your code, that gives result what you expect.

if($string =~ /<(TEXT)>\s*(.*)\s*<\/\1>/gs)


's' -> Treat string as single line.

k_manimuthu
Junior Poster in Training
93 posts since Jun 2009
Reputation Points: 55
Solved Threads: 24
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: