简体   繁体   English

不能在 Perl 上用括号分割字符串

[英]Can't split a string by a parenthesis on Perl

For example the following code:例如下面的代码:

$test_str = 'foo(bar';
@arr = split('(', $test_str);

causes the 500 error导致 500 错误

Why?为什么?

As ikegami says , split expects a pattern as its first argument.正如池上所说split期望一个模式作为它的第一个参数。 A string will just be converted into a pattern.一个字符串只会被转换成一个模式。 Because an open parenthesis ( has a special meaning, this will error. You need to escape it.因为一个左括号(有特殊含义,这会出错。你需要转义它。

my @arr = split /\(/, $str;

According to perldoc -f split , the first argument to the split() function is a regular expression /PATTERN/ .根据perldoc -f splitsplit()函数的第一个参数是正则表达式/PATTERN/ So if you were to write this:所以如果你要写这个:

split('some text', $string)

it would be equivalent to:它相当于:

split( m/some text/, $string )

And if some text contains characters that are special in regular expressions, then they will treated as such.如果some text包含正则表达式中的特殊字符,那么它们将被视为特殊字符。 So your line:所以你的线路:

@arr = split('(', $test_str);

will be treated as:将被视为:

@arr = split( m/(/, $test_str );

This is likely not what you want, as m/(/ is an invalid (you could say incomplete) regular expression. To match on a literal ( , you need to escape it with a back-slash, so use this instead:这可能不是您想要的,因为m/(/是无效的(您可以说是不完整的)正则表达式。要匹配文字( ,您需要使用反斜杠对其进行转义,因此请改用它:

@arr = split( m/\(/, $test_str );

By now you've noticed that Perl tries to be helpful by converting your first argument from the string '(' to the regular expression pattern m/(/ . Although you can pass in a string as the first argument, I don't recommend it -- use m/PATTERN/ instead.现在你已经注意到 Perl 试图通过将你的第一个参数从字符串'('转换为正则表达式模式m/(/ 。虽然你可以传入一个字符串作为第一个参数,但我不建议它——使用m/PATTERN/代替。

The reason for my recommendation is because:我推荐的理由是:

  1. A pattern makes it clear that the first argument is a regular expression pattern, and not just any old string.模式清楚地表明第一个参数是正则表达式模式,而不仅仅是任何旧字符串。
  2. According to perldoc -f split , there is a special case where you can pass in a string as the first argument:根据perldoc -f split ,有一种特殊情况,您可以将字符串作为第一个参数传入:

As a special case, specifying a PATTERN of space (' ') will split on white space just as "split" with no arguments does.作为一种特殊情况,指定空格 (' ') 的 PATTERN 将在空白处拆分,就像没有参数的“拆分”一样。 Thus, "split(' ')" can be used to emulate awk's default behavior, whereas "split(/ /)" will give you as many null initial fields as there are leading spaces.因此,"split(' ')" 可用于模拟 awk 的默认行为,而 "split(//)" 将为您提供与前导空格一样多的空初始字段。 A "split" on "/\\s+/" is like a "split(' ')" except that any leading whitespace produces a null first field. "/\\s+/" 上的 "split" 类似于 "split(' ')",只是任何前导空格都会产生一个空的第一个字段。 A "split" with no arguments really does a "split(' ', $_)" internally.没有参数的“拆分”实际上在内部执行“拆分(' ', $_)”。

It's good not to confuse the two.最好不要混淆两者。 So use ' ' as the first argument to split() when you want to use the special case of splitting on whitespace, and use m/PATTERN/ as the first argument for every other case.因此,当您想使用空格分割的特殊情况时,使用' '作为split()的第一个参数,并使用m/PATTERN/作为其他情况的第一个参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM