Now I've tried various patterns \b[1.2.3]\b which matches if 1 or 2 or 3 is present.
Is there something I can use to say AND?
I've tried && with no result.
If you want to match '123' in that order then you do not want to use the brackets as they represent a character class. So, if you wanted to find '123' in the string "Hey, it's as easy as 123!" you might use something like /123/
There are some some regular expression tutorial links in this thread that you may want to review.
[webmasterworld.com...]
I've been reading the Perl Black Book and documentation on the net including learn.perl.org
What I want to do is check if I have three vowels in a string so I came up with this pattern:
Enter pattern: m/a{3}¦e{3}¦i{3}¦o{3}¦u{3}/g
Enter string: this is a testing time
Nope!
then I tried the following and thought OK I'm close but when I persisted and tested all of them I got down to uuu and aaa and got a perculiar result.
Next string: eee
We have a match
Next string: ooo
We have a match
Next string: iii
We have a match
Next string: uuu
Nope!
Next string: aaa
Nope!
Why am I getting a match on eee, ooo,iii but not aaa or uuu?
Why does it not find three i's in my first test string
Mike
I've tried the pattern you suggest is it the way I'm doing it in code that is wrong?
Here is a extract:
if ($string =~ m/$pattern/){
print "We have a match\n";
} else {
print "Nope!\n";
}
print "Next string: ";
Mike
:-)
#! C:\perl\bin\perl
use warnings;
use strict;
my ($pattern, $string);
print "Enter pattern: ";
chomp($pattern = <STDIN>);
print "Enter string: ";
while (<STDIN>){
chomp($string = $_);
if ($string =~ m/$pattern/){
print "We have a match\n";
} else {
print "Nope!\n";
}
print "Next string: ";
}
Mike
I have a pattern which will split a web address the code looks like this:
my ( $proto, $host, $port, $path, $query, $frag);
$proto = '(\w*)';
$host = '([\w\.]*)';
$port = ':(\d*)';
$path = '([\w/\.]*)';
$query = '\?([\w=]*)';
$frag = '#(\w*)';
while(<STDIN>){
chomp;
my $url = "${proto}://${host}${port}?${path}${query}?${frag}?";
my @answers = m/$url/;
print ("Protocol = $answers[0]\n");
print ("Host = $answers[1]\n");
}
My proble is I get nothing in my @answers array.
Is there a simple thing I'm missing or am I way off track here.
Thank you for your patience.
my expression looks like this:
(\w*)://([\w\.]*):(\d*)([\w/\.]*)\?([\w=]*)#(\w*)
[test.com...]
which means the \1 = \w* or http
\2 = [\w\.]* or www.test.com
\3 = \d* nothing in this case
\4 = [\w/\.]* or test.cgi
in my code $url = (\w*)://([\w\.]*):(\d*)([\w/\.]*)\?([\w=]*)#(\w*)
If I key in [test.com...] I would expect to get at least \1 \2 and \4 but I get nothing in the $host.
I guess the question is how do I apply $_ against my pattern in the $url scalar so each pattern picks up the correct value.
[edited by: coopster at 3:24 am (utc) on Mar. 24, 2005]
[edit reason] Disabled graphic smile faces for this post [/edit]
The? can make it optional so if I use the following:
(\w*)://([\w\.]*):?(\d*)([\w/\.]*)\?([\w=]*)#?(\w*)
It will work if I have a data there or I don't.
I have been using Regex Coach which is a good tool and I get a match with or without the port etc.
So I applied it to my program and it works, although I have yet to test it for a wide range of possibilities.
Thansk for your persisting with the "nothing makes it optional" as the penny finaly dropped.
ab?c = an a followed by an optional b followed by a c; that is, either abc or ac appear in many tutorials but the obvious is sometimes hidden in plain sight.
Mike
(\w*)://([\w\.]*):?(\d*)/?([\w/\.]*)
with [test.com:...]
I should get in $1 http in $2 www.test.com and a null in $3 and in $4 path.
Mike
used on
[test.com:80...]
gives me the following groups
¦http¦www.test.com¦80¦path/more_path¦who=what¦fragment
The only issue I now have is I've lost the 'optional' part for both port :80 and Path /
I want to validate the url and fail it if it has an : which is not followed by a number and fail it if the path is not preceded by a /.
While I can get the thing to work as optional as soon as I put in the look back and look ahead it forces these to be included rather than optional.
Any hint about how better to use the look ahead or look behind?
[search.cpan.org...]
I'm getting close but no cigar at this point. Any other resources you could recommend. I have the Perl Black Book, the Camel Book and some notes from various internet sites. I need to look at the perlretut documentation which I'll do tonight.
Mike
(\w*)://([\w\.]*):?([?=\d+]\d*)?/?([?=\w+][\w/\.]*)?\?([\w=]*)?#?(\w*)?
The issue I have is if I want the port to be optional I include the? marks. How do I indicate that if the : appears we must have a port, ie numbers following.
With the above and a test url equal to :
[test.com:?p=o#frag...]
I get the following result:
¦http¦www.test.com¦?¦p¦=o¦frag
Where? is the port p is the path =0 the query and frag the fragment.