简体   繁体   English

正则表达式用于过滤不以单词结尾的字符串

[英]Regex for filtering strings not ending in a word

I am trying to write a regex for filtering out perforce changelist descriptions which are not pending. 我试图编写一个正则表达式,以过滤掉未决的perforce变更列表描述。

So string " Change 358618 on 2013/11/09 by abcdm@abcd_PQRST_UVWXY " should pass but string " Change 358618 on 2013/11/09 by abcdm@abcd_PQRST_UVWXY *pending* " should not. 因此字符串“ 由abcdm @ abcd_PQRST_UVWXY更改对2013年11月9日358618”应该通过,但串“ 由abcdm @ abcd_PQRST_UVWXY于2013年11月9日更改358618 *未决*”不应该。 So far i have come up with the following. 到目前为止,我已经提出了以下建议。

($token1,$token2,$token3) = ( $string =~ /^Change\s+([0-9]+)\s+(.*)\s(?!\\*pending\\*)/ );

So if the pattern matches i should be able to consume the changelist number. 因此,如果模式匹配,我应该能够使用变更列表编号。 If the changelist is pending, no tokens will be returned. 如果更改列表待处理,则不会返回任何令牌。

在表达式的末尾使用否定的后置断言:

(?<!\*pending\*)$

I would not solve this problem using Regex as it is an expensive operation. 我不会使用正则表达式解决此问题,因为这是一项昂贵的操作。 If you are certain that the strings that you wish to exclude ends with pending just check that part of the string using offsets: 如果确定要排除的字符串以挂起结尾,请使用偏移量检查字符串的该部分:

$string1 = 'Change 358618 on 2013/11/09 by abcdm@abcd_PQRST_UVWXY *pending*';
$string2 = '*pending*';

$offset = length($string1) - length($string2);

$result = index($string1, $string2, $offset);

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

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