++ for effort, oko1, but you may wish to consider a couple small points:
For example:
String: now is the time
Regex: /\stime/
Result: now is the time
Matched: Yes
might be uninformative to the newcomer who hasn't yet groked much about regexen (and, specifically, about the need to use capturing parens in your program to obtain appropriate feedback). OTOH, of course, it works splendidly for this (in which our presumed newcomer has come to understand captures, even if s/he can't follow your code):
String: now time
Regex: /now\s(time)/
Result: now time
Matched: Yes
Captures: [#1: 'time']
So, Good to go! for those who have at least some of the basics... but:
My bad! See alexm's correction, below.
String: fred same joe time
Regex: ?time?
The regex must be a valid match or a substitute expression.
String: |time|
Regex: |time|
The regex must be a valid match or a substitute expression.
String: fred same time
Regex: ~time~
The regex must be a valid match or a substitute expression.
String: fred same time
Regex: !time!
The regex must be a valid match or a substitute expression.
Perhaps a brief addition to the use_instruction (line 10) specifying the use of /.../ as the acceptable regex delimiter for input to this script (or re-coding to accept the usual alternatives?) and a bit of clarification about the output would make your script an even more valuable tool. | [reply] [d/l] [select] |
String: fred same joe time
Regex: m?time?
Result: fred same joe time
Matched: Yes
String: fred same joe time
Regex: m~time~
Result: fred same joe time
Matched: Yes
String: fred same joe time
Regex: m|time|
Result: fred same joe time
Matched: Yes
String: fred same joe time
Regex: m!time!
Result: fred same joe time
Matched: Yes
| [reply] [d/l] [select] |
| [reply] |
Wow, very nice. I'm going to play around with that one for a bit. I just started learning regex's in depth, so this is a great tool. I have one I constructed as well, which might be useful, if just a simple match test is your aim:
#!usr/bin/perl
$my_string = "";
$my_regx = "";
# This program tests regular expressions against a match.
if ($my_string =~ /$my_regx/x)
print "Matched: " . "\"" . $& . "\"" . "\n"; # print matc
+h
print "Before match: " . "\"" . $` . "\"" . "\n"; # print data
+ before match
print "After match: " . "\"" . $' . "\"" . "\n"; # print data
+ after match
}else {
# do nothing
} # end if
print "Rockstar Programming Inc. All Rights Reserved\n";
So, now I have yours to explore with and maybe my much simpler "matcher" could possibly be of use to you :) | [reply] [d/l] |
There are a few things here that could use a little tidying. First off, the mandatory mantra: Always use strictures (use strict; use warnings; - see The strictures, according to Seuss).
Of more immediate benefit: if you find you need to include double quote character in a string use qq for quoting instead of " (see perlop's Quote and Quote-like Operators). For example your print statements become:
print qq{Matched: "$&"\n}; # print match
print qq~Before match: "$`"\n~; # print data before match
print qq|After match: "$'"\n|; # print data after match
although you may wish to use a here-doc instead (look for here-doc in the same docs mentioned above):
print <<STUFF;
Matched: "$&"
Before match: "$`"
After match: "$'"
STUFF
Perl reduces RSI - it saves typing
| [reply] [d/l] [select] |