[英]Check for spaces in perl using regex match in perl
I have a variable how do I use the regex in perl to check if a string has spaces in it or not ? 我有一个变量,如何在perl中使用正则表达式检查字符串中是否包含空格? For ex:
例如:
$test = "abc small ThisIsAVeryLongUnbreakableStringWhichIsBiggerThan20Characters";
So for this string it should check if any word in the string is not bigger than some x characters. 因此,对于此字符串,应检查字符串中是否有不超过x个字符的单词。
#!/usr/bin/env perl
use strict;
use warnings;
my $test = "ThisIsAVeryLongUnbreakableStringWhichIsBiggerThan20Characters";
if ( $test !~ /\s/ ) {
print "No spaces found\n";
}
Please make sure to read about regular expressions in Perl. 请确保阅读有关Perl中的正则表达式的信息。
Perl regular expressions tutorial - perldoc perlretut
Perl正则表达式教程
perldoc perlretut
You should have a look at the perl regex tutorial . 您应该看一下perl regex教程 。 Adapting their very first "Hello World" example to your question would look like this:
根据他们的问题改编他们的第一个“ Hello World”示例将如下所示:
if ("ThisIsAVeryLongUnbreakableStringWhichIsBiggerThan20Characters" =~ / /) {
print "It matches\n";
}
else {
print "It doesn't match\n";
}
die "No spaces" if $test !~ /[ ]/; # Match a space
die "No spaces" if $test =~ /^[^ ]*\z/; # Match non-spaces for entire string
die "No whitespace" if $test !~ /\s/; # Match a whitespace character
die "No whitespace" if $test =~ /^\S*\z/; # Match non-whitespace for entire string
To find the length of the longest unbroken sequence of non-space characters, write this 要查找最长的不间断非空格字符序列的长度,请编写以下代码
use strict;
use warnings;
use List::Util 'max';
my $string = 'abc small ThisIsAVeryLongUnbreakableStringWhichIsBiggerThan20Characters';
my $max = max map length, $string =~ /\S+/g;
print "Maximum unbroken length is $max\n";
output 输出
Maximum unbroken length is 61
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.