Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

regex 1

Status
Not open for further replies.

cginewbie

Programmer
Joined
Jan 20, 2002
Messages
24
Location
US
another regex help
if I have a field name "whatever" and I want to check to see if the input has any words that is greater than 10 in length and reject it. How do I do that in one regex...

I could do it by split it out into tokens and check every token it is not effiecent...
 
length <some string> will return the number of bytes of a scalar value. Since characters are represented as one byte each, you now have the length of a string.

HTH,
Johnny
 
You misunderstood my question....
the input would consist of a series of words, not only one word... so your suggest does not work
What I want to check is to see if any of the input words inside the string is more than XX character in length, not the whole string...

I can do it by break it into tokens, anc check every tokens... What I want to know if there is a better way to check them in one regex
such as
flag an error if(/[0-9a-zA-Z]{,xx}/)
 
Sorry I've been doing that a lot lately....

If you want to check every word in the string try:
my $string = '<sentence>';
while ( $string =~ /([A-Z0-9]+)/ig )
{
&<Some Error Function> if ( ( length $1 ) > xx);
}
 
You could do it like this;

while(<FILE>) {

@sentence = split; #splits into individual words on whitespace.

for (@sentence) {

next if length $_ < xx;
error_func($_); # this will be called if lentgth $_ > XX
}
 
{
local $\ = undef;
local $/ = undef;
open (FILE, &quot;<$file&quot;);
$line = <FILE>;
$line =~ s/(\w{20,})/&func($1)/ge;
close FILE;
}
 
if ($string =~ /([A-z0-9]{,xx} )+/gi)
{
print &quot;error&quot;;
}
should work too. //Daniel
 
Code:
if ( $string =~ /\b\w{10,}\b/ ) {
   # found a word 10 chars or longer
}
Matches a word boundary, 10 or more &quot;word&quot; characters, and another word boundary Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
Status
Not open for further replies.

Similar threads

Replies
2
Views
352
  • Locked
  • Question Question
Replies
4
Views
479
  • Locked
  • Question Question
Replies
1
Views
320
  • Locked
  • Question Question
Replies
5
Views
463

Part and Inventory Search

Sponsor

Back
Top