Regular Expression Question Why Doesn't this Match?

J

jm

/([\w\s\.\'\-]*)

does not match this:

My Fictional Co.

But it does match

Product by abc company

I don't understand why it matches the second one, but not the first.
Doesn't my expresionsay match anything that has a word, space, dot,
apostrophe, or a hyphen, one or more times? Thefirst one has a . in it
and it should work. It does work on others like:

Dr. Smith's

Thank you for any help.
 
D

Dr.Ruud

jm schreef:
/([\w\s\.\'\-]*)

thaT iS brokeN.

does not match this:

My Fictional Co.

witH thE missinG enD-slasH addeD, it doeS:

echo 'My Fictional Co.' |
perl -nwle '/([\w\s.\x27-]*)/ and print qq{<$1>}'

This prints:
<My Fictional Co.>
 
J

jm

Christian said:
jm said:
/([\w\s\.\'\-]*)

does not match this:

My Fictional Co.

But it does match

Product by abc company

I don't understand why it matches the second one, but not the first.
Doesn't my expresionsay match anything that has a word, space, dot,
apostrophe, or a hyphen, one or more times? Thefirst one has a . in it
and it should work. It does work on others like:

The RE above isn't closed, and I doubt that it is your
complete expression. Give a short, working examples that
illustrates what exactly you are trying to accomplish,
and what goes wrong. Then people will be able to aid in
solving your problem.

Otherwise, to me the pattern seems to match just fine:
C:\> perl
use strict; use warnings;
$_='My Fictional Co.';
print 'Matches: '.$1.$/ if( /([\w\s\.\'\-]*)/ );
^Z

Matches: My Fictional Co.
C:\>

-Chris

I forgot to put the / in my example above, but it is the complete
expression. I am trying ot match a URL for url rewriting. One of the
products has the format like My Fictional Co. but it won't match it.
 
J

jm

Dr.Ruud said:
jm schreef:
/([\w\s\.\'\-]*)

thaT iS brokeN.

does not match this:

My Fictional Co.

witH thE missinG enD-slasH addeD, it doeS:

echo 'My Fictional Co.' |
perl -nwle '/([\w\s.\x27-]*)/ and print qq{<$1>}'

This prints:
<My Fictional Co.>

--
Another URL that doesn't match has the format:

A.B.C.D. Just won't match it. I did forget the / in my original
question. Sorry.
 
T

Tad McClellan

jm said:
Christian said:
jm said:
/([\w\s\.\'\-]*)

does not match this:

My Fictional Co.

But it does match

Product by abc company

I don't understand why it matches the second one, but not the first.


I don't understand how it could fail to match the first...



If you do that for the case that you claims "does not match" that
expression, then we can help figure it out.

If you don't, we can't.

I forgot to put the / in my example above, but it is the complete
expression. I am trying ot match a URL


Then aren't there some angle brackets or something else in the
pattern that you are actually using?

for url rewriting. One of the
products has the format like My Fictional Co. but it won't match it.


Yes it will.

Your pattern above will match every possible string!

Show real code along with real data in order to receive real help.
 
T

Tad McClellan

jm said:
Dr.Ruud said:
jm schreef:
/([\w\s\.\'\-]*)

thaT iS brokeN.

does not match this:

My Fictional Co.

witH thE missinG enD-slasH addeD, it doeS:

echo 'My Fictional Co.' |
perl -nwle '/([\w\s.\x27-]*)/ and print qq{<$1>}'

This prints:
<My Fictional Co.>

--
Another URL that doesn't match has the format:

A.B.C.D. Just won't match it.


It works for me:

---------------------------
#!/usr/bin/perl
use warnings;
use strict;

$_ = 'A.B.C.D.';
print "matched\n" if /([\w\s\.\'\-]*)/;
 

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,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top