to parse a string

C

cc

Hi,

I am new in Perl. Here is my question for help.

Given a string, e.g. "01010123", any sample code to scan it and then
find the "2" and "3" are not qualified in the string which requires
only "0" and "1"?

Thanks,

mzc.
 
A

A. Sinan Unur

I am new in Perl. Here is my question for help.

Then this is a good time to read the posting guidelines for this group
to learn how you can help yourself, and help others help you.

Please note that "write my code for me" requests are not very popular
here.
Given a string, e.g. "01010123", any sample code to scan it and then
find the "2" and "3" are not qualified in the string which requires
only "0" and "1"?

Read about character classes and regular expressions:

perldoc perlre

perldoc perlreref

perldoc perlop

Sinan
 
C

cc

I am new in Perl. Here is my question for help.
Then this is a good time to read the posting guidelines for this group
to learn how you can help yourself, and help others help you.

Please note that "write my code for me" requests are not very popular
here.

Clearly, this is not what I wanted either; and this is why my sample
question from.
Read about character classes and regular expressions:

perldoc perlre

perldoc perlreref

perldoc perlop

I tried them before this email. Thank you anyway; can anybody else let
me know where I can find those kind of good general sample codes?
Thanks.
 
A

A. Sinan Unur

Clearly, this is not what I wanted either; and this is why my sample
question from.

I am not sure what you mean by the above sentence, but you did not
include any code.
let me know where I can find those kind of good general sample codes?

You'd be better off trying to do it, and asking for help with specific
issues.

Sinan
 
R

robic0

If you have perl on your system, try to print out the
perlcheat.html. On there there are Regex metachars.
the [] denote a character class, so if [1a6], if any of these
individual
characters are contained in what yer checking it is a match.
also, '-' is a metachar inside a [] meaning a span of chars
so [a-z] is all the chars from a through z.

You want to match 2 and/or 3 there, but not 0 or 1. So you want
to match [2-9]. Put a couple of forward slashes around it and
you have a regex. You might want to read up on it.

use strict;
use warnings;

my $numstr = '01010123';

if ($numstr =~ /[2-9]/) {
print "bad numbers: $numstr\n";
$numstr =~ s/[2-9]/-/g;
print "replaced: $numstr\n";
}

__DATA__

bad numbers: 01010123
replaced: 010101--
 
T

Tad McClellan

cc said:
Given a string, e.g. "01010123", any sample code to scan it and then
find the "2" and "3" are not qualified in the string which requires
only "0" and "1"?


The usual idiom for validating data is:

anchor the beginning.

anchor the ending.

write a regex in between that accounts for everything
that you want to allow.


print "'$str' is bad data\n" unless $str =~ /^[01]+$/;
 
R

robic0

On Thu, 27 Oct 2005 21:59:01 -0500, Tad McClellan

Well ya know Tad, i wanted to slightly introduce him to
metacharacters [] since he might get scared of all the
professionalism around hear
cc said:
Given a string, e.g. "01010123", any sample code to scan it and then
find the "2" and "3" are not qualified in the string which requires
only "0" and "1"?


The usual idiom for validating data is:

anchor the beginning.

anchor the ending.

write a regex in between that accounts for everything
that you want to allow.


print "'$str' is bad data\n" unless $str =~ /^[01]+$/;
 
T

Tassilo v. Parseval

Also sprach Tad McClellan:
The usual idiom for validating data is:

anchor the beginning.

anchor the ending.

Why the anchoring?
write a regex in between that accounts for everything
that you want to allow.


print "'$str' is bad data\n" unless $str =~ /^[01]+$/;

Or use tr:

print "'$str' is bad data\n" if $str =~ tr/^01//c;

Tassilo
 
J

John W. Krahn

Tassilo said:
Also sprach Tad McClellan:
The usual idiom for validating data is:

anchor the beginning.

anchor the ending.

Why the anchoring?
write a regex in between that accounts for everything
that you want to allow.


print "'$str' is bad data\n" unless $str =~ /^[01]+$/;

Or use tr:

print "'$str' is bad data\n" if $str =~ tr/^01//c;
^
Are you sure that caret is supposed to be there?


John
 
A

Anno Siegel

John W. Krahn said:
Tassilo said:
Also sprach Tad McClellan:
Given a string, e.g. "01010123", any sample code to scan it and then
find the "2" and "3" are not qualified in the string which requires
only "0" and "1"?

The usual idiom for validating data is:

anchor the beginning.

anchor the ending.

Why the anchoring?
write a regex in between that accounts for everything
that you want to allow.


print "'$str' is bad data\n" unless $str =~ /^[01]+$/;

Or use tr:

print "'$str' is bad data\n" if $str =~ tr/^01//c;
^
Are you sure that caret is supposed to be there?

Yes, in Middle-High-Perl where double negation is the norm.

Anno
 

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,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top