Easy regular expression problem - matching characters and newline at the same time 
Author Message
 Easy regular expression problem - matching characters and newline at the same time

Hi!

I've got this text:

$text = "<html>Ek, Jesper/Bladh, Camilla<br>\nHaftad.\nPagina f?rlags
AB, \nSverige, \n2002\n<nobr>(ISBN:?9163607069).</nobr></td>";

I want to extract everything that comes after "Camilla<br>" and before
"</nobr>". It can contain any character or newline characters (as
above).

I've tried this:

$text =~ /Camilla<br>(.*)<nobr>/m;
$result = $1;

But, it doesn't work. I think it's because the dot only matches
characters. I just want anything between, even a newline character.
How can I do that?

Thank you for your help!

/ Jacob



Thu, 28 Oct 2004 22:25:37 GMT  
 Easy regular expression problem - matching characters and newline at the same time

Quote:

> I've got this text:

> $text = "<html>Ek, Jesper/Bladh, Camilla<br>\nHaftad.\nPagina f?rlags
> AB, \nSverige, \n2002\n<nobr>(ISBN:&nbsp;9163607069).</nobr></td>";

> I want to extract everything that comes after "Camilla<br>" and before
> "</nobr>". It can contain any character or newline characters (as
> above).

> I've tried this:

> $text =~ /Camilla<br>(.*)<nobr>/m;
> $result = $1;

> But, it doesn't work. I think it's because the dot only matches
> characters. I just want anything between, even a newline character.
> How can I do that?

Try this instead:

   ($result) = $text =~ m#Camilla<br>\n(.*)</nobr>#s;

The /s attribute makes the regex treat the string as a single line (so the
dot also matches a newline.)

HTH,
Andre



Fri, 29 Oct 2004 00:00:32 GMT  
 Easy regular expression problem - matching characters and newline at the same time

  JN> $text = "<html>Ek, Jesper/Bladh, Camilla<br>\nHaftad.\nPagina
  JN> f?rlags AB, \nSverige,
  JN> \n2002\n<nobr>(ISBN:&nbsp;9163607069).</nobr></td>";

  JN> I want to extract everything that comes after "Camilla<br>" and
  JN> before "</nobr>". It can contain any character or newline
  JN> characters (as above).

  JN> $text =~ /Camilla<br>(.*)<nobr>/m; $result = $1;

why the /m? there are no anchors in there and /m only modifies anchors.

  JN> But, it doesn't work. I think it's because the dot only matches
  JN> characters. I just want anything between, even a newline
  JN> character.  How can I do that?

you are so close. if you look where you found /m you will find another
modifier that makes . match all characters including newline.

uri

--

-- Stem is an Open Source Network Development Toolkit and Application Suite -
----- Stem and Perl Development, Systems Architecture, Design and Coding ----
Search or Offer Perl Jobs  ----------------------------  http://jobs.perl.org



Thu, 28 Oct 2004 23:51:44 GMT  
 Easy regular expression problem - matching characters and newline at the same time
use 's' instead of 'm'


Quote:
> Hi!

> I've got this text:

> $text = "<html>Ek, Jesper/Bladh, Camilla<br>\nHaftad.\nPagina f?rlags
> AB, \nSverige, \n2002\n<nobr>(ISBN:&nbsp;9163607069).</nobr></td>";

> I want to extract everything that comes after "Camilla<br>" and before
> "</nobr>". It can contain any character or newline characters (as
> above).

> I've tried this:

> $text =~ /Camilla<br>(.*)<nobr>/m;
> $result = $1;

> But, it doesn't work. I think it's because the dot only matches
> characters. I just want anything between, even a newline character.
> How can I do that?

> Thank you for your help!

> / Jacob



Fri, 29 Oct 2004 09:04:57 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Regular Expressions: Matching words contained within character patterns?

2. Matching special characters in a regular expression

3. Match any character w/ regular expressions

4. Regular expression problem (should be easy)

5. Problem with matching with variable regular expression

6. Problem with Regular Expression Match

7. newbie problem with *s (regular expression matching in win32)

8. Regular expressions and newlines

9. Pattern matching newline character

10. Q: Easy regular expression available for this...

11. Easy Regular Expression

12. Regular Expression Help (Easy Question)

 

 
Powered by phpBB® Forum Software