简体   繁体   English

PHP将字符串与正则表达式匹配,如果匹配的字符串后没有数字

[英]PHP Match the string to regex, if after the matched string are no digits

Lets say that I have a excel function like that IF(A1>0,1,2) I match it by pregex and by dictionary I translate it into Czech language ( KDYŽ(A1>0,1,2) ). 可以说我有一个excel函数,例如IF(A1>0,1,2)我通过pregex匹配它,并通过字典将其翻译成捷克语( KDYŽ(A1>0,1,2) )。 When I would translate it back I should get the same string, but instead of it I get IF(AND1>0,1,2) . 当我将其翻译回时,我应该得到相同的字符串,但是我得到的是IF(AND1>0,1,2) Because "A" is in Czech "AND" which is one of key words in excel function. 因为“ A”是捷克语中的“ AND”,所以它是excel函数中的关键字之一。

So I need to grep all letters while after them is no number (= not cell name), how to achieve that while I'm using this code to get array of matched words? 所以我需要在所有字母都没有数字(=不是单元格名称)之后对所有字母进行grep,当我使用此代码获取匹配单词的数组时如何实现呢?

$text = strtolower("KDYŽ(A1>0,1,2)");

if (preg_match_all('/[a-zěščřžýáíéúůóďťň]+/i',$text,$match)) {
  /* matched text should be:
  $match[0] = "KDYŽ";
  no $match[1] = "A" should exists;*/
}

Use 采用

'/[a-zěščřžýáíéúůóďťň]+[^0-9]/i'

which maches your characters not followed by a 0-9. 这会使您的字符不跟在0-9之间。 You'd need to strip the last character from your match though since the first character following the string you search will be returned, too. 您还需要从匹配中删除最后一个字符,因为搜索字符串后面的第一个字符也将返回。

Or, try 或者,尝试

'/[a-zěščřžýáíéúůóďťň]+(?![0-9])/i'

which is a negative look-ahead. 这是负面的预见。

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

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