CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Regular Expressions:
Match characters in middle and end string

 

First page Previous page 1 2 Next page Last page  View All


rovf
Veteran

Jan 19, 2013, 1:48 PM

Post #26 of 31 (9150 views)
Re: [Stefanik] Match characters in middle and end string [In reply to] Can't Post


Quote
So, what I need is to match "....SUB....[^;]"


This would mean matching already if *any* non-semicolon character comes somewhere after the SUB. Don't think that you really mean that.

You said, that it should match even if there is no newline "at the end". But what, then, is the end? Do you mean "the end of the string"?

I think the main problem here is that I still don't understand exactly under what conditions you want your string to be matched....


Stefanik
User

Jan 20, 2013, 6:41 AM

Post #27 of 31 (9130 views)
Re: [rovf] Match characters in middle and end string [In reply to] Can't Post

For Laurent:
yes is exactly what I need, but I don't understand few codes:

Why do you execute:

Code
chomp $qpar;

the you repeat chomp in next instruction code?


Code
$qpar .= <DATA> and chomp $qpar while ($qpar !~ /;\s*$/);

what is ".=" ?

for rovf:
sorry, maybe I was just confusing to explain.
I have a log file where all the relevant lines have word "SUB" at the beginning of line (but not the first characters) and ending with semicolon.
In some case this line is split on more lines, so I have this string start at line and end in the next line (where semicolon is).
I have to "normalize" this situation before to print them.
Last problem is that ";" could have a "\n", or not (if it's at the end of file, whitout any other line next).


rovf
Veteran

Jan 21, 2013, 1:50 AM

Post #28 of 31 (9083 views)
Re: [Stefanik] Match characters in middle and end string [In reply to] Can't Post

In this case, I would ignore the \n completely. Just find SUB, followed by any text, up the next semicolon. You just need to make sure that the dot matches the newline, otherwise your pattern will faile. I.e. you need something like


Code
/(SUB.+?;)/ms


(Note the 's' modifier to the regexp!)


Laurent_R
Veteran / Moderator

Jan 21, 2013, 4:49 AM

Post #29 of 31 (9081 views)
Re: [rovf] Match characters in middle and end string [In reply to] Can't Post

Hi,


Code
$qpar .= <DATA> and chomp $qpar while ($qpar !~ /;\s*$/);


This says: if $qpar does not end with a semi-colon (;) possibly followed by some spaces, then get the next line of input, concatenate it with the current $qpar, chomp the new $qpar (this is needed since a new line was added at the end of $qpar, you need to remove the new line characters again), and do all this as long as the new line you get is not ended by a semi-colon.

$c .= "foo" : this takes $c and concatenates "foo" at the end of $c.

This is equivalent to $c = $c . "foo";


Stefanik
User

Jan 21, 2013, 1:24 PM

Post #30 of 31 (9060 views)
Re: [Laurent_R] Match characters in middle and end string [In reply to] Can't Post

Hi,
thanks to all of you for your helps and explain.

rovf, just a question again... what is "s" at the end of regexp?

Ste


(This post was edited by Stefanik on Jan 21, 2013, 1:25 PM)


Laurent_R
Veteran / Moderator

Jan 22, 2013, 1:57 PM

Post #31 of 31 (9040 views)
Re: [Stefanik] Match characters in middle and end string [In reply to] Can't Post

Perl documentation on Regex modifiers:

- m :
Treat string as multiple lines. That is, change "^" and "$" from matching the start or end of the string to matching the start or end of any line anywhere within the string.

- s :
Treat string as single line. That is, change "." to match any character whatsoever, even a newline, which normally it would not match.

Used together, as /ms, they let the "." match any character whatsoever, while still allowing "^" and "$" to match, respectively, just after and just before newlines within the string.

First page Previous page 1 2 Next page Last page  View All
 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives