简体   繁体   English

用于识别关键字中字符的正则表达式

[英]Regex to identify characters in keyword

I want to examine 1 or more characters that constitute a keyword.我想检查构成关键字的 1 个或多个字符。

So if the the keyword is "show", then s, sh, sho, or show all qualify but all other combinations would fail.因此,如果关键字是“show”,则 s、sh、sho 或 show 都符合条件,但所有其他组合都会失败。

I'm thinking look-ahead is the solution but unsure how to make them optional and still enforce the requirement.我认为先行是解决方案,但不确定如何使它们成为可选的并仍然执行要求。

As in..就像在..

echo "s" |回声“s” | perl -ne 'print if /s(?=h)?(?=o)?(?=w)?/' should print perl -ne 'print if /s(?=h)?(?=o)?(?=w)?/' 应该打印

echo "sh" |回声“嘘” | perl -ne 'print if /s(?=h)?(?=o)?(?=w)?/' should print perl -ne 'print if /s(?=h)?(?=o)?(?=w)?/' 应该打印

echo "sho" |回声“笑” | perl -ne 'print if /s(?=h)?(?=o)?(?=w)?/' should print perl -ne 'print if /s(?=h)?(?=o)?(?=w)?/' 应该打印

echo "show" |回声“显示” | perl -ne 'print if /s(?=h)?(?=o)?(?=w)?/' should print perl -ne 'print if /s(?=h)?(?=o)?(?=w)?/' 应该打印

and

echo "st" |回声“圣” | perl -ne 'print if /s(?=h)?(?=o)?(?=w)?/' should fail perl -ne 'print if /s(?=h)?(?=o)?(?=w)?/' 应该失败

echo "sto" |回声“斯托” | perl -ne 'print if /s(?=h)?(?=o)?(?=w)?/' should fail perl -ne 'print if /s(?=h)?(?=o)?(?=w)?/' 应该失败

echo "stop" |回声“停止” | perl -ne 'print if /s(?=h)?(?=o)?(?=w)?/' should fail perl -ne 'print if /s(?=h)?(?=o)?(?=w)?/' 应该失败

etc. ETC。

Reverse think it.反过来想想。 Use使用

'show' =~ m{^$keyword$}

Use index rather than a regex:使用index而不是正则表达式:

perl -nle 'print if index("show", $_) == 0'

( -l removes the newline from $_ and adds one after print ) -l$_中删除换行符并在print后添加一个)

This one-liner will print the input if it's a prefix of show (ie, if the input is a substring of show that starts at index 0 ).如果输入是show的前缀(即,如果输入是从索引0开始的show的 substring),则此单行代码将打印输入。


If you really need a regex, I would suggest:如果你真的需要一个正则表达式,我会建议:

/^s(h(ow?)?)?$/

(use (?: instead of the ( if performance of capture groups matter: it's basically the same thing except that it doesn't capture the group) (使用(?:而不是(如果捕获组的性能很重要:它基本上是一样的,只是它不捕获组)

This kind of regex should be fairly easy to build programmatically with a recursive function:这种正则表达式应该很容易使用递归 function 以编程方式构建:

sub build_re {
  my ($first, $end) = split //, $_[0], 2;
  return $first if $end eq "";
  return "$first(" . build_re($end) . ")?";
}

my $re = build_re("show");  # prints s(h(o(w)?)?)?

print "s" =~ /^$re$/ ? 1 : 0; # 1
print "sh" =~ /^$re$/ ? 1 : 0; # 1
print "show" =~ /^$re$/ ? 1 : 0; # 1

print "showw" =~ /^$re$/ ? 1 : 0; # 0
print "how" =~ /^$re$/ ? 1 : 0; # 0

The 3rd argument of split ( 2 ) tells split to only split in 2 fields rather than "as many as possible" (the default). split ( 2 ) 的第三个参数告诉split只分割成 2 个字段而不是“尽可能多”(默认)。 This way, this split splits the input of build_re into "1st character" and "the rest".这样,这种splitbuild_re的输入拆分为“第一个字符”和“其余字符”。 It's somewhat equivalent to my ($first, $end) = $_[0] =~ /^(.)(.*)$/ (assuming that the input is on a single line).它有点等同于my ($first, $end) = $_[0] =~ /^(.)(.*)$/ (假设输入在一行上)。

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

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