On Sunday, Oct 31, 2004, at 21:11 US/Central, John W. Krahn wrote:
Robert Citek wrote:On Saturday, Oct 30, 2004, at 20:52 US/Central, John W. Krahn wrote:[EMAIL PROTECTED] wrote:How married are you to using a regular expression? An alternative may be to use split:I have to match patterns of the format
string1>string2
where the strings 1 & 2 can contain alphabets,numbers and spaces. The
string are separated by '>' sign. I wrote the following code for this.
if(/([a-z]*[A-Z]*[0-9]*[\s]*)>([a-z]*[A-Z]*[\s]*[0-9]*)/g) {
$string1 = $1;
$string2 = $2;
}
This picks up only the first character in string 2 whereas I want
everything till the end of the $_ to be in the string. $_ is terminated
by \n.
I cannot understand what I am missing in the regular expression.
my ( $string1, $string2 ) = /^([[:alnum:][:blank:]]*)>([[:alnum:][:blank:]]*)$/
my ($string1, $string2) = split(">", $_, 2) ;
That is still using a regular expression.
Yup. You're right.
(The first argument to split() *IS* a regular expression or will be evaluated as a regular expression.) By your logic, if I change my expression to this I will not be using a regular expression:
my ( $string1, $string2 ) = $_ =~ '^([[:alnum:][:blank:]]*)>([[:alnum:][:blank:]]*)$'
I just don't think of literal text as a regular expression, although it still is. For example, if I want to grep using literal text, I use fgrep.
Also, using split() does not restrict the input to only alphanumeric and space characters as the OP requested.
Without sample input data, I can't be sure what the restrictions were. For example, it is not quite clear how these cases should be handled:
$_=">"; $_="abc>"; $_=">def";
Depending on the case, either string1 or string2 or both will not contain alphanums or blanks. Also, there may be strings that the OP does want to capture and that do contain symbols:
$_="USB stick > $50.00";
Without some sample data, we are all making reasonable, but possibly incorrect, guesses.
BTW, I tried this code with your RE and got an error using perl 5.6:
$ perl -e ' $_=">" ; if (/^([[:alnum:][:blank:]]*)>([[:alnum:][:blank:]]*)$/) { print "$1, $2\n" } ' Character class [:blank:] unknown at -e line 1.
Worked fine on perl 5.8.
Regards, - Robert http://www.cwelug.org/downloads Help others get OpenSource. Distribute FLOSS for Windows, Linux, *BSD, and MacOS X with BitTorrent
-- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>