regexp

  • Thread starter Wojtek Michalik
  • Start date
W

Wojtek Michalik

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
 
W

Walter Roberson

: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.

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

??
 
W

Wojtek Michalik

: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.

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----------
 
J

John Bokma

Wojtek said:
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.
 
C

ctcgag

Wojtek Michalik said:
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
 
C

ctcgag

Wojtek Michalik said:
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
 
D

David K. Wall

Wojtek Michalik said:
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.
:)
 
J

Joe Smith

Wojtek said:
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
 

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

Members online

Forum statistics

Threads
473,769
Messages
2,569,577
Members
45,052
Latest member
LucyCarper

Latest Threads

Top