Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > regexp

Reply
Thread Tools

regexp

 
 
Wojtek Michalik
Guest
Posts: n/a
 
      06-03-2004
Can anybody give me en example of working regex
that will match sample text given below:
____________
banana\n
\tapple\n
____________
\t=tab and \n=nweline
I have been trying to match it by:
perl -pi -e 's#(banana\n\t)(apple)#$1\.\/$2#g' with modifiers like m s ms
nothing worked.
I use perl version 5.8 on linux
 
Reply With Quote
 
 
 
 
Walter Roberson
Guest
Posts: n/a
 
      06-03-2004
In article <(E-Mail Removed)>,
Wojtek Michalik <(E-Mail Removed)> wrote:
:Can anybody give me en example of working regex
:that will match sample text given below:
:____________
:banana\n
:\tapple\n
:____________
:\t=tab and \n=nweline
:I have been trying to match it by:
erl -pi -e 's#(banana\n\t)(apple)#$1\.\/$2#g' with modifiers like m s ms
:nothing worked.

You haven't really defined what your pattern is.

I would ask, though, why you are using the g modifier? Is this all part
of a larger string that there might be several such matches on? If so
then what happens if there is a line strawberry-banana in there? You
haven't anchored the banana portion.

I would also ask why you escaped the period and slash in your replacement
pattern? In replacement patterns, period and slash stand for themselves
[unless one of them happens to be the pattern delimeter.]

To confirm: the output you would expect over those lines would be

banana\n
\t./apple\n

??
--
Aleph sub {Aleph sub null} little, Aleph sub {Aleph sub one} little,
Aleph sub {Aleph sub two} little infinities...
 
Reply With Quote
 
 
 
 
Wojtek Michalik
Guest
Posts: n/a
 
      06-03-2004
On Thu, 03 Jun 2004 17:22:33 +0000, Walter Roberson wrote:

> In article <(E-Mail Removed)>,
> Wojtek Michalik <(E-Mail Removed)> wrote:
> :Can anybody give me en example of working regex
> :that will match sample text given below:
> :____________
> :banana\n
> :\tapple\n
> :____________
> :\t=tab and \n=nweline
> :I have been trying to match it by:
> erl -pi -e 's#(banana\n\t)(apple)#$1\.\/$2#g' with modifiers like m s ms
> :nothing worked.
>
> You haven't really defined what your pattern is.
>
> I would ask, though, why you are using the g modifier?


Because I am a perl newbie. Don't know much about it.

>Is this all part
> of a larger string that there might be several such matches on? If so
> then what happens if there is a line strawberry-banana in there?


Can be, I will deal with it. The only -I hope - problem I have, is newline inside my pattern

> You haven't anchored the banana portion.


Why is it so important ???

> I would also ask why you escaped the period and slash in your replacement
> pattern? In replacement patterns, period and slash stand for themselves
> [unless one of them happens to be the pattern delimeter.]


Often don't remember the obove - that's why.

> To confirm: the output you would expect over those lines would be
>
> banana\n
> \t./apple\n
>
> ??

------exactly----------

 
Reply With Quote
 
John Bokma
Guest
Posts: n/a
 
      06-03-2004
Wojtek Michalik wrote:

> On Thu, 03 Jun 2004 17:22:33 +0000, Walter Roberson wrote:


>>I would ask, though, why you are using the g modifier?

>
> Because I am a perl newbie. Don't know much about it.


So that means you just try something without actually reading what it
means? You saw somewhere g and though, well, let's try it.

Regexpen are not made on trail and error basis but with careful thinking.

--
John MexIT: http://johnbokma.com/mexit/
personal page: http://johnbokma.com/
Experienced Perl programmer available: http://castleamber.com/
Happy Customers: http://castleamber.com/testimonials.html
 
Reply With Quote
 
ctcgag@hotmail.com
Guest
Posts: n/a
 
      06-03-2004
"Wojtek Michalik" <(E-Mail Removed)> wrote:
> Can anybody give me en example of working regex
> that will match sample text given below:
> ____________
> banana\n
> \tapple\n
> ____________


When including literal text in a post, quote it the way you would
quote it in Perl. Otherwise it is ambiguous. It appears that there are
two newlines between "banana" and "apple", because "\n" represents one
newline, and the actual newline (which causes them to be on separate lines)
represents another newline. Well, assuming the above is in double-quotish
circumstances, which reading further, I suspect it isn't.

> \t=tab and \n=nweline


Maybe it does, maybe it doesn't. Since it looks like you are using
the the i switch, I'm guessing that in fact \t represents '\t' and \n
represents '\n'.


> I have been trying to match it by:
> perl -pi -e 's#(banana\n\t)(apple)#$1\.\/$2#g' with modifiers like m s ms


Dealing with shell special characters and escaping, as well as perl special
characters and escaping, is a maddening proposition. If you can't make
your -e command-liner work, turn it into a real script before debugging.
Once you get your script to work, then you try turning it back into a
one-liner.

Also, simplify the substitution into a simple match. Once you get that
to work, you can go back to the substitution.

Finally, have you tried simply printing the input with so you can see what
it actually looks like?

Xho

--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service $9.95/Month 30GB
 
Reply With Quote
 
ctcgag@hotmail.com
Guest
Posts: n/a
 
      06-03-2004
"Wojtek Michalik" <(E-Mail Removed)> wrote:
> Can anybody give me en example of working regex
> that will match sample text given below:
> ____________
> banana\n
> \tapple\n
> ____________


When including literal text in a post, quote it the way you would
quote it in Perl. Otherwise it is ambiguous. It appears that there are
two newlines between "banana" and "apple", because "\n" represents one
newline, and the actual newline (which causes them to be on separate lines)
represents another newline. Well, assuming the above is in double-quotish
circumstances, which reading further, I suspect it isn't.

> \t=tab and \n=nweline


Maybe it does, maybe it doesn't. Since it looks like you are using
the the i switch, I'm guessing that in fact \t represents '\t' and \n
represents '\n'.

> I have been trying to match it by:
> perl -pi -e 's#(banana\n\t)(apple)#$1\.\/$2#g' with modifiers like m s ms


Dealing with shell special characters and escaping, as well as perl special
characters and escaping, is a maddening proposition. If you can't make
your -e command-liner work, turn it into a real script before debugging.
Once you get your script to work, then you try turning it back into a
one-liner.

Also, simplify the substitution into a simple match. Once you get that
to work, you can go back to the substitution.

Finally, have you tried simply printing the input without modification so
you can see what it actually looks like?

Xho

--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service $9.95/Month 30GB
 
Reply With Quote
 
David K. Wall
Guest
Posts: n/a
 
      06-03-2004
Wojtek Michalik <(E-Mail Removed)> wrote:

> Can anybody give me en example of working regex
> that will match sample text given below:
> ____________
> banana\n
> \tapple\n
> ____________
> \t=tab and \n=nweline
> I have been trying to match it by:
> perl -pi -e 's#(banana\n\t)(apple)#$1\.\/$2#g' with modifiers like
> m s ms nothing worked.
> I use perl version 5.8 on linux


In addition to the other advice you've been given, a closer look at the
-p option could be beneficial. You're reading one line at a time (I
don't see $/ being redefined anywhere) and trying to match over
multiple lines. Somehow I'm not surprised that success is eluding you.

 
Reply With Quote
 
Joe Smith
Guest
Posts: n/a
 
      06-04-2004
Wojtek Michalik wrote:

> perl -pi -e 's#(banana\n\t)(apple)#$1\.\/$2#g' with modifiers like m s ms


That won't work because using -p reads the file one line at a time.
This means that is stops at each \n. The first time through, $_ will
have "banana\n" and the second time $_ will have "\tapple\n".
In neither case will \n and \t be consecutive characters.

Since the pattern you're looking for spans lines, you'll need to
slurp the entire file into a scalar variable before looking for it.

Use the command
perldoc perlvar
and look for the section on $INPUT_RECORD_SEPARATOR for an example
for using slurp mode.
-Joe
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
new RegExp().test() or just RegExp().test() Matěj Cepl Javascript 3 11-24-2009 02:41 PM
[regexp] How to convert string "/regexp/i" to /regexp/i - ? Joao Silva Ruby 16 08-21-2009 05:52 PM
Ruby 1.9 - ArgumentError: incompatible encoding regexp match(US-ASCII regexp with ISO-2022-JP string) Mikel Lindsaar Ruby 0 03-31-2008 10:27 AM
Programmatically turning a Regexp into an anchored Regexp Greg Hurrell Ruby 4 02-14-2007 06:56 PM
RegExp.exec() returns null when there is a match - a JavaScript RegExp bug? Uldis Bojars Javascript 2 12-17-2006 09:59 PM



Advertisments