Leading Space with REGEX

J

jerryacoleman

I have the following regex. It is matching what I want( /BIG/TRUCK),
but it is also included the leading space. Is there anyway to
suppress the leading space.

\b[^"|']/\w+/\w+\b

String:

TYPE /BIG/TRUCK
 
P

Paul Lalli

I have the following regex. It is matching what I want( /BIG/TRUCK),
but it is also included the leading space. Is there anyway to
suppress the leading space.

\b[^"|']/\w+/\w+\b

String:

TYPE /BIG/TRUCK

I don't understand your question. You're very specifically looking
for "word boundary; anything other than double quote, vertical-bar, or
single quote; slash; one or more word characters; slash; one or more
word characters; word boundary". The space character matches the
"anything other than..." portion of that. If you don't want to match
that space character, why are you putting that token in there?

To put it another way: Please post a SHORT but COMPLETE script that
demonstrates both what you're doing, and the output that demonstrates
what you want to do.

Paul Lalli
 
J

jgraber

Paul Lalli said:
I have the following regex. It is matching what I want( /BIG/TRUCK),
but it is also included the leading space. Is there anyway to
suppress the leading space.

\b[^"|']/\w+/\w+\b

String:

TYPE /BIG/TRUCK

I don't understand your question. You're very specifically looking
for "word boundary; anything other than double quote, vertical-bar, or
single quote; slash; one or more word characters; slash; one or more
word characters; word boundary". The space character matches the
"anything other than..." portion of that. If you don't want to match
that space character, why are you putting that token in there?

To put it another way: Please post a SHORT but COMPLETE script that
demonstrates both what you're doing, and the output that demonstrates
what you want to do.

Paul Lalli

Jerry, and all other people with Regex type questions;
If you read the posting guidelines that are posted frequently
and easily googleable for this news group, you'll see that what
Paul means by "SHOFT but COMPLETE program"
is something like this:

==cut==
use warnings; # see perldoc warnings
use strict; # see perldoc strict
while (<DATA>){ # get a line from below __DATA__ in to $_ default var
chomp;
my ($saved) = ( m:(\b[^"|']/\w+/\w+\b): );
print "Line $. is '$_', saved section is '$saved'\n";
}
__DATA__
TYPE /BIG/TRUCK
ECHO /SMALL/CAR
==cut==

# Here is the output I have:
Line 1 is 'TYPE /BIG/TRUCK', saved section is ' /BIG/TRUCK'
Line 2 is 'ECHO /SMALL/CAR', saved section is ' /SMALL/CAR'

# Here is the output I desire:
Line 1 is 'TYPE /BIG/TRUCK', saved section is '/BIG/TRUCK'
Line 2 is 'ECHO /SMALL/CAR', saved section is '/SMALL/CAR'

######
With the above type of data, it is easy for anyone to
copy and paste your complete program with DATA and run it
to demonstrate. Plus, it helps you find the problem yourself.

Hey look, if I follow Paul's adice and move the ( to after
the [] section, it generates the desired output.
was my ($saved) = ( m:(\b[^"|']/\w+/\w+\b): );
now my ($saved) = ( m:\b[^"|'](/\w+/\w+\b): );
^------>(
 
J

John W. Krahn

I have the following regex. It is matching what I want( /BIG/TRUCK),
but it is also included the leading space. Is there anyway to
suppress the leading space.

\b[^"|']/\w+/\w+\b

String:

TYPE /BIG/TRUCK

\b matches at six places in the above example, between the beginning of the
string and the 'T' in TYPE, between the 'E' in TYPE the ' ' following it,
between the '/' and the 'B' in BIG, between the 'G' in BIG and the '/',
between the '/' and the 'T' in TRUCK, and finally, between the 'K' in TRUCK
and the end of the string.

In your example the only place the the first \b can match is between the 'E'
in TYPE the ' ' following it and the second \b is superfluous because \w+ is
greedy so anything following it will not match \w.



John
 

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,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top