Re: matching a defined lenght
by duff (Parson) on Apr 23, 2007 at 12:38 UTC
|
I'll assume by your use of the word "match" that you're looking for regular expression help. A dot (.) will match any character and the curly brace quantifier will allow you to match any number of the preceding thing. However, dot does not match the newline sequence, so perhaps you need a /s modifier on your RE. Put these things together and you get something like this:
$string =~ /.{30}/s;
See perlre and perlretut for more information.
If you didn't really mean to use regular expressions, see the length function.
| [reply] [d/l] [select] |
|
|
I have tried with =~ m/.{30}/
but it does not seem to work under windows
activestate Perl
is this possible
rgds
Harry
| [reply] |
|
|
Didn't you accidentally omit the capturing parentheses? Shouldn't it be written as /(.{30})/ instead? You don't need m// here, since you didn't change the standard delimiters (//).
But we're still unkowingly what you like to achieve, since your description was a bit ambiguous and you didn't show us some excerpts from your code in question.
| [reply] [d/l] [select] |
|
|
I have tried with =~ m/.{30}/
but it does not seem to work under windows
activestate Perl
is this possible
If that is a question (I can't see the question mark) then the answer is no. Since others already gave examples to the effect that it does work, perhaps you should show possibly with an actual example what it is that makes you think if fails to. Without explicit code one can only make guesses and since you used the word "defined", a possible one may be that you're expecting the above to match only on a string of exactly 30 charachters. But it will match also on a longer one, because it will match a string of exactly 30 charachters on a string of 30 or more.
Another possibility could be that the string you want to match on has newlines in it, and then you will need the s modifier.
Last, without further specifications, we may suspect an XY problem here, which may not be the case, but if you say explicitly what you're after, then we could be sure.
| [reply] [d/l] [select] |
Re: matching a defined lenght
by Zaxo (Archbishop) on Apr 23, 2007 at 12:36 UTC
|
I don't know exactly what you're trying to do, but substr will get you there. If you really don't care what's in it,
my $match = substr $string, 0, 30;
extracts the first thirty characters.
If you need some real matching, an expression like
substr($string, 0, 30) =~ /$re/
works fine. substr is an lvalue function, so that even works for substitution.
| [reply] [d/l] [select] |
Re: matching a defined lenght
by shigetsu (Hermit) on Apr 23, 2007 at 12:46 UTC
|
I'm not so sure whether I grasp the correct connotation of 'match' in that context. But, consider using:
use strict;
use warnings;
my $str = 'abc' x 10;
if (length $str == 30) {
print "\$str has length of 30\n";
}
or
print $str =~ /(.{30})/;
Former one succeeds if $str has a length of 30 characters. Latter one yields back the first 30 characters extracted from $str using a regular expression.
| [reply] [d/l] [select] |
Re: matching a defined lenght
by MonkE (Hermit) on Apr 23, 2007 at 12:56 UTC
|
Several others have taken a stab at answering your question. I hope they answered the question you really had in mind. However since your question was a bit vague, it seems to me that you might want to look at the following guides for posting questions:
- [How (Not) To Ask A Question]
- [How do I post a question effectively?]
| [reply] |
Re: matching a defined lenght
by jhourcle (Prior) on Apr 23, 2007 at 14:04 UTC
|
substr $string, 0, 30;
# will extract UP TO the first 30 characters of the string.
$string =~ m/(.{30})/;
# will extract the first 30 characters, if the string is AT LEAST 30
+ characters long
$string =~ m/.{30}/;
# tests if the string is AT LEAST 30 characters long
length($string) == 30;
# tests if the string is 30 characters long.
the last two are different in that return true/false, without trying to extract the first 30 characters, but they'll give different results for greater than 30 characters ... the first two are different at less than 30 characters:
| [reply] [d/l] [select] |
Re: matching a defined lenght
by Moron (Curate) on Apr 23, 2007 at 13:14 UTC
|
A string won't always function as a regexp to find itself. What people mean by using substr instead is to do something like:
sub subpos { # find the offset of any substring
my ( $checkstring, $substring ) = @_;
my $length = length( $substring );
my $limit = length( $checkstring ) - $length;
for ( my $i = 0; $i < $limit; $i++ ) {
( substr( $checkstring, $i, $length ) eq $substring )
and return $i;
}
undef(); # or alternatively return -1 for not found
}
length.
__________________________________________________________________________________
^M Free your mind!
| [reply] [d/l] |
Re: matching a defined lenght
by Herkum (Parson) on Apr 23, 2007 at 17:06 UTC
|
I found it bizarre that no one mentioned using length.
if (length $var == 30) {
print "It is 30 characters long";
}
else {
print "It is not 30 characters long";
}
| [reply] [d/l] |
Re: matching a defined lenght
by GrandFather (Saint) on Apr 23, 2007 at 20:49 UTC
|
| [reply] |
Re: matching a defined lenght
by jdporter (Paladin) on Apr 23, 2007 at 23:05 UTC
|
While we're gratuitously flogging our own nodes, may I suggest that perhaps you have an XY Problem?
| [reply] |
|
|
| [reply] |