problem with multiline regular expression

G

Grzegorz Danowski

Hi,

I'd like to read all lines of caption text from a string:
....
Name ="Paragraph"
Caption ="The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dog. "
"The quick brown fox jumps over the lazy dog. The
quick brown fox jumps over the lazy dog. The qu"
"ick brown fox jumps over the lazy dog. The quick
brown fox jumps over the lazy dog."
FontName ="Arial CE"
....

First I used simple expression:
(?<=Caption\s=)".*?"
It worked wrongly because it gave only for first line of caption string. I
thought I should check if prefix is not present and I simply modified my
regex to:
(?<=Caption\s=)".*?"(?<!\r\n\s*)

But the expression give the same result as previous - only first line of
caption string.
Meantime I tested another expression:
dog.\s"\r\n\s*".*"
And It worked as I expected (gave one last word from first caption line and
then whole second line). What is proper way to solve my problem?
--
Regards,
Grzegorz

Ps. I tested all my expressions using Expresso 3.0 with checked options:
"Ignore Case", "Ignore White", "Multiline".
 
G

Grzegorz Danowski

Grzegorz Danowski said:
Hi,

I'd like to read all lines of caption text from a string:
...
Name ="Paragraph"
Caption ="The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dog. "

Better formated example:
Name ="Paragraph"
Caption ="The quick brown fox jumps over the lazy dog. "
"The quick brown fox jumps over the lazy dog. The qui"
"ck brown fox jumps over the lazy dog."
FontName ="Arial CE"

And question: what regex should I use to get out all Catpion text (in the
example three sentences "The quick brown fox...")?
I have tried it: (?<=Caption\s=)".*?"(?<!\r\n\s*)

Regards,
Grzegorz
 
K

Kevin Spencer

(?i)(?<=caption\s*=\s*)"[^"]+

First, I made the regular expression case-insensitive. Then I added a "Zero
or more" space expression both before and after the equals sign. Then comes
the quotation mark, followed by a character class indicating 1 or more
characters that are NOT a quotation mark. This captures everything between
the quotation marks following "caption=" with or without spaces around the
equals sign.

--
HTH,

Kevin Spencer
Microsoft MVP

Printing Components, Email Components,
FTP Client Classes, Enhanced Data Controls, much more.
DSI PrintManager, Miradyne Component Libraries:
http://www.miradyne.net
 
G

Grzegorz Danowski

Thanks for your help, but it only parse first line of caption string...
--
Regards,
Grzegorz

Kevin Spencer said:
(?i)(?<=caption\s*=\s*)"[^"]+

First, I made the regular expression case-insensitive. Then I added a
"Zero or more" space expression both before and after the equals sign.
Then comes the quotation mark, followed by a character class indicating 1
or more characters that are NOT a quotation mark. This captures everything
between the quotation marks following "caption=" with or without spaces
around the equals sign.

--
HTH,

Kevin Spencer
Microsoft MVP

Printing Components, Email Components,
FTP Client Classes, Enhanced Data Controls, much more.
DSI PrintManager, Miradyne Component Libraries:
http://www.miradyne.net

Grzegorz Danowski said:
Hi,

I'd like to read all lines of caption text from a string:
...
Name ="Paragraph"
Caption ="The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dog. "
"The quick brown fox jumps over the lazy dog. The
quick brown fox jumps over the lazy dog. The qu"
"ick brown fox jumps over the lazy dog. The quick
brown fox jumps over the lazy dog."
FontName ="Arial CE"
...

First I used simple expression:
(?<=Caption\s=)".*?"
It worked wrongly because it gave only for first line of caption string.
I thought I should check if prefix is not present and I simply modified
my regex to:
(?<=Caption\s=)".*?"(?<!\r\n\s*)

But the expression give the same result as previous - only first line of
caption string.
Meantime I tested another expression:
dog.\s"\r\n\s*".*"
And It worked as I expected (gave one last word from first caption line
and then whole second line). What is proper way to solve my problem?
--
Regards,
Grzegorz

Ps. I tested all my expressions using Expresso 3.0 with checked options:
"Ignore Case", "Ignore White", "Multiline".
 
G

Grzegorz Danowski

Grzegorz Danowski said:
Better formated example:
Name ="Paragraph"
Caption ="The quick brown fox jumps over the lazy dog. "
"The quick brown fox jumps over the lazy dog. The qui"
"ck brown fox jumps over the lazy dog."
FontName ="Arial CE"

And question: what regex should I use to get out all Catpion text (in the
example three sentences "The quick brown fox...")?
I have tried it: (?<=Caption\s=)".*?"(?<!\r\n\s*)

I have written another expression:

(?<=Caption\s=)(?:\s*(?:".*"))+

And I believe that it works :)
 
J

Jesse Houwing

* Grzegorz Danowski wrote, On 28-4-2007 19:25:
Hi,

I'd like to read all lines of caption text from a string:
...
Name ="Paragraph"
Caption ="The quick brown fox jumps over the lazy
dog. The quick brown fox jumps over the lazy dog. "
"The quick brown fox jumps over the lazy dog. The
quick brown fox jumps over the lazy dog. The qu"
"ick brown fox jumps over the lazy dog. The quick
brown fox jumps over the lazy dog."
FontName ="Arial CE"
...

First I used simple expression:
(?<=Caption\s=)".*?"
It worked wrongly because it gave only for first line of caption string.
I thought I should check if prefix is not present and I simply modified
my regex to:
(?<=Caption\s=)".*?"(?<!\r\n\s*)

But the expression give the same result as previous - only first line of
caption string.
Meantime I tested another expression:
dog.\s"\r\n\s*".*"
And It worked as I expected (gave one last word from first caption line
and then whole second line). What is proper way to solve my problem?

There's an option you can pass to the RegEx object called SingleLine.
This will cause the parser to treat . as any character. The default is
any character except newline.

Jesse
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top