how to match or detect alphanumeric including - or spaces through out

J

Jack

Hi

I have strings : 'SA-QA TAR TEL-LL' and 'SA QA-TA REG TEL-LL'
and I want to test that this is a valid alphanumeric ; I will have
strings which vary the space and - position. Looking to find a matcher
that allows for a 0 or 1 spaces and 0 or more dashes throughout without
overly complicating the regex.. I tried this but it started getting too
case specific and complex:

$temp = 'SAQA TAR sd-f44TELLL';
if ($temp =~ m/^\w+\s?\w*\-*\w*\s?\w*\-*\w+\s?\w+$/) { print "
alphanum "; } #
# can try this also ! m/^\w+\s*w+$/

Does anyone have a solution that could handle all combinations of
alphanumeric and -, space

thank you

Jack
 
P

Paul Lalli

Jack said:
I have strings : 'SA-QA TAR TEL-LL' and 'SA QA-TA REG TEL-LL'
and I want to test that this is a valid alphanumeric ;

You need to better define what you mean. An "alphanumeric" is either
an alphabetic or numeric character. Dashes and spaces are not
alphanumerics. Therefore, your strings are not "valid alphanumeircs".
So what do you actually mean?
I will have
strings which vary the space and - position. Looking to find a matcher
that allows for a 0 or 1 spaces and 0 or more dashes throughout without
overly complicating the regex.. I tried this but it started getting too
case specific and complex:

$temp = 'SAQA TAR sd-f44TELLL';
if ($temp =~ m/^\w+\s?\w*\-*\w*\s?\w*\-*\w+\s?\w+$/) { print "
alphanum "; } #
# can try this also ! m/^\w+\s*w+$/

Does anyone have a solution that could handle all combinations of
alphanumeric and -, space

I don't at all understand what you're *trying* to do, but the answer to
this question is:
m/^[\w\s-]+$/

If that doesn't apply to your situation, you need to post a
short-but-complete script, along with sample input, desired output, and
actual output, that demonstrates the problems you're having. Be sure
to include at least a couple strings that should match and a couple
that should not.

Paul Lalli
 
X

Xicheng Jia

Jack said:
Hi

I have strings : 'SA-QA TAR TEL-LL' and 'SA QA-TA REG TEL-LL'
and I want to test that this is a valid alphanumeric ; I will have
strings which vary the space and - position. Looking to find a matcher
that allows for a 0 or 1 spaces and 0 or more dashes throughout without
overly complicating the regex.. I tried this but it started getting too
case specific and complex:

$temp = 'SAQA TAR sd-f44TELLL';
if ($temp =~ m/^\w+\s?\w*\-*\w*\s?\w*\-*\w+\s?\w+$/) { print "
alphanum "; } #
# can try this also ! m/^\w+\s*w+$/

Does anyone have a solution that could handle all combinations of
alphanumeric and -, space

I supposed you want to find strings which are concatenation of words by
non-repeating whitespace or hyphen, and meanwhile the string can not
contain leading/trailing whitespaces or hyphenes. So:

"This is-a valid-string"
"This is- not a valid string" # space and hyphen connected
"not--valid" # repeating of hyphenes
"not valid" # repeating of spaces
"-not valid" # leading hyphen
"not valid " # trailing space

If the above is what you wanted, then you are asking a FAQ regex
question, and your pattern can be as following:

^\w+(?:[\s-]\w+)*$

If you allow repeating hyphenes. but no connection between any hyphen
and space, then change [\s-] to (?:\s|-+).........

If still not, please make your question clearer, and provide us with
valid/invalid sample data.

Xicheng
 
B

Ben Morrow

Quoth "Paul Lalli said:
Jack said:
Does anyone have a solution that could handle all combinations of
alphanumeric and -, space

I don't at all understand what you're *trying* to do, but the answer to
this question is:
m/^[\w\s-]+$/

That also matches _, which is not commonly considered alphanumeric.

/^[[:alnum:]\s-]+$/

or

/^[[:alnum:] -]+$/

depending on whether your definition of 'space' includes tab/CR/LF or
not.
If that doesn't apply to your situation, you need to post a
short-but-complete script, along with sample input, desired output, and
actual output, that demonstrates the problems you're having. Be sure
to include at least a couple strings that should match and a couple
that should not.

Seconded :)

Ben
 
J

John W. Krahn

Jack said:
I have strings : 'SA-QA TAR TEL-LL' and 'SA QA-TA REG TEL-LL'
and I want to test that this is a valid alphanumeric ; I will have
strings which vary the space and - position. Looking to find a matcher
that allows for a 0 or 1 spaces and 0 or more dashes throughout without
overly complicating the regex.. I tried this but it started getting too
case specific and complex:

$temp = 'SAQA TAR sd-f44TELLL';
if ($temp =~ m/^\w+\s?\w*\-*\w*\s?\w*\-*\w+\s?\w+$/) { print "
alphanum "; } #
# can try this also ! m/^\w+\s*w+$/

Does anyone have a solution that could handle all combinations of
alphanumeric and -, space

/\A[[:alnum:]][[:alnum:] -]*[[:alnum:]]\z/



John
 
X

Xicheng Jia

John said:
Jack said:
I have strings : 'SA-QA TAR TEL-LL' and 'SA QA-TA REG TEL-LL'
and I want to test that this is a valid alphanumeric ; I will have
strings which vary the space and - position. Looking to find a matcher
that allows for a 0 or 1 spaces and 0 or more dashes throughout without
overly complicating the regex.. I tried this but it started getting too
case specific and complex:

$temp = 'SAQA TAR sd-f44TELLL';
if ($temp =~ m/^\w+\s?\w*\-*\w*\s?\w*\-*\w+\s?\w+$/) { print "
alphanum "; } #
# can try this also ! m/^\w+\s*w+$/

Does anyone have a solution that could handle all combinations of
alphanumeric and -, space

/\A[[:alnum:]][[:alnum:] -]*[[:alnum:]]\z/

Hi, John:

this won't match a stand-alone alnum, say $str = "A";
guess he may want to use lookaround for this purpose. :)

Xicheng
 
B

Ben Morrow

Quoth "Xicheng Jia said:
John said:
Jack said:
I have strings : 'SA-QA TAR TEL-LL' and 'SA QA-TA REG TEL-LL'
and I want to test that this is a valid alphanumeric ; I will have
strings which vary the space and - position. Looking to find a matcher
that allows for a 0 or 1 spaces and 0 or more dashes throughout without
overly complicating the regex.. I tried this but it started getting too
case specific and complex:

$temp = 'SAQA TAR sd-f44TELLL';
if ($temp =~ m/^\w+\s?\w*\-*\w*\s?\w*\-*\w+\s?\w+$/) { print "
alphanum "; } #
# can try this also ! m/^\w+\s*w+$/

Does anyone have a solution that could handle all combinations of
alphanumeric and -, space

/\A[[:alnum:]][[:alnum:] -]*[[:alnum:]]\z/

this won't match a stand-alone alnum, say $str = "A";
guess he may want to use lookaround for this purpose. :)

No need for that:

/\A [[:alnum:]] (?: [[:alnum:] -]* [[:alnum:]] )? \z/x

Ben
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top