Char position of 1st non-word char in a string 
Author Message
 Char position of 1st non-word char in a string

I would like to find the first non-word character in a string. I tried:

$x='abcdef:gh';
$ipos=index $x,"\W",0;
print $ipos;

and similar things, but it won't work.
In this test I'm expecting to print 6, the offset of the colon(:).

Thanks for your help.



Wed, 18 Jun 1902 08:00:00 GMT  
 Char position of 1st non-word char in a string
[posted & mailed]

Quote:

> I would like to find the first non-word character in a string. I tried:

> $x='abcdef:gh';
> $ipos=index $x,"\W",0;
> print $ipos;

Did you look up index in the manual to see why this didn't work?  \W
only has meaning in regexes.  So use a regex and pos().

    if ($x =~ /\W/g) { $ipos = pos($x) - 1 }

or if you don't like subtracting

    if ($x =~ /(?=\W)/g) { $ipos = pos($x) }

perldoc -f pos
perldoc -f index

Of course, if you really want to find the first non-word character and
not it's position in the string then don't use the above.

    print $1 if $x =~ /(\W)/;

--
Rick Delaney



Wed, 18 Jun 1902 08:00:00 GMT  
 Char position of 1st non-word char in a string

Quote:

>I would like to find the first non-word character in a string. I tried:

>$x='abcdef:gh';
>$ipos=index $x,"\W",0;
>print $ipos;

>and similar things, but it won't work.
>In this test I'm expecting to print 6, the offset of the colon(:).

Heh. index() only searches for literal strings, not regexes.

The old-fashioned way would be to use a simple regex, and check $` , the
string that comes before the match.

    $ipos = ($x =~ /\W/) ? length($`) : -1;

The disadvantage is that this gives a speed penalty for ALL regexes in
your script, because a value is assigned to $` whether you need it or
not.

Alernatively, you can use:

    $ipos = ($x =~ /^(.*?)\W/) ? length($1) : -1;

which may be just a bit slower for this regex, but has no penalty for
any other regexes.

--
        Bart.



Wed, 18 Jun 1902 08:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Uppercase 1st char of each word???

2. RegEx: Capitalize 1st char. of string with spaces

3. how to define alphanumeric (words containing non-ASCII chars)

4. reading char by char in a string

5. Appending char to strings by char

6. Trying to extra 1st 20 chars of line

7. @chars the same as ($#chars) ?

8. Replacing special chars with special chars

9. Q: using a c-lib with char** and even char***

10. s/Hex/Char/ ? s/Char/Hex/

11. Why no regex char class for punc chars???

12. Replacing multiple occurences of newline chars with a single newline char

 

 
Powered by phpBB® Forum Software