简体   繁体   English

正则表达式-从固定模式中排除符号

[英]regex - exclude symbol from fixed pattern

I need a help from some regular expression expert. 我需要一些正则表达式专家的帮助。

I have a script which give a fixed list of pattern. 我有一个脚本,提供了固定的模式列表。 With very simple regular expression i search for pattern existence in target text. 通过非常简单的正则表达式,我可以搜索目标文本中是否存在模式。 For example: 例如:

/.*TFT.*/
/.*LCD.*/
/.*ANTI-GLARE.*/

Those strings - TFT,LCD,ANTI-GLARE are fixed - I can't modify them. 那些字符串-TFT,LCD,ANTI-GLARE是固定的-我无法对其进行修改。 Well, the problem is in ANTI-GLARE - that dash will cause not to match AntiGlare. 好吧,问题出在反眩光中-破折号将导致不匹配AntiGlare。 I know that id I make regex like this: 我知道我像这样制作正则表达式的ID:

/.*ANTI-?GLARE.*/

this will match both variants, but i can't modify that string. 这将匹配两个变体,但我不能修改该字符串。 My question is: Is there another way to make symbol optional, than exclamation mark in pattern? 我的问题是:除了图案中的感叹号之外,还有没有其他方法可以使符号可选?

EDIT - clearance 编辑-清除

After some answers and hints, i'll try to give more specific description of my problem. 经过一些答案和提示后,我将尝试对我的问题进行更具体的描述。 I have a strings in table: 我在表中有一个字符串:

Anti Glare 抗眩光

TFT
AntiGlare
LCD

Via XML I receive new model which description is like: 通过XML,我收到一个新的模型,其描述如下:

Anti-Glare 抗眩光

i need to ensure - did i have a entry with "Anti Glare" like description. 我需要确保-我是否输入了带有“防眩光”的说明。 I can not change that Anti-Glare received, because there a lot of strings - vary in wide range - it's impossible to build logical group or any like this in regex. 我无法更改Anti-Glare收到的内容,因为字符串很多-范围很广-无法在正则表达式中建立逻辑组或类似的组。 it's build with php script like: 它是使用php脚本构建的,例如:

$rexp="/.*~.*/i"; 
  • where ~ is replaced with that string. 〜替换为该字符串。

    $rexp=str_replace('~',$rexp,$cond); $ rexp = str_replace('〜',$ rexp,$ cond);

  • where $cond holds current string received $ cond保存当前收到的字符串

    Operator can change any part outside that ~, but can not in ~ content - there will be a thousands of them 操作员可以更改〜以外的任何部分,但不能更改〜的内容-将会有成千上万的内容

You're using php, so we can assume you've got something like an array with every strings accepted. 您使用的是php,因此我们可以假设您已经接受了每个字符串,例如数组。

Here is a way to do what you want, if only dash bother you : 这是一种做您想要的事情的方法,如果只是破折号会打扰您:

<?php

$words = array('TFT', 'LCD', 'ANTI-GLARE');
$pattern = '/(' . str_replace('-', '-?', implode($words, '|')) . ')/i';

?>

This will generate you the pattern /(TFT|LCD|ANTI-?GLARE)/i , which will fit your needs . 这将为您生成模式/(TFT|LCD|ANTI-?GLARE)/i它会满足您的需求

EDIT 编辑

As mentioned in comments, you're receiving every possible strings : TFT;LCD;AntiGlare;Anti-Glare;Anti Glare . 如评论中所述,您将收到所有可能的字符串: TFT;LCD;AntiGlare;Anti-Glare;Anti Glare You then just have to concatenate them with a pipe (the OR operator in regex) and wrap he whole block with parenthesis : 然后,您只需用管道将它们连接起来(正则表达式中的OR运算符),并用括号将整个块包装起来:

$words = array('TFT', 'LCD', 'AntiGlare', 'Anti-Glare', 'Anti Glare');
$pattern = '/(' . implode($words, '|') . ')/i';

This will generate you the pattern /(TFT|LCD|AntiGlare|Anti-Glare|Anti Glare)/i 这将为您生成图案/(TFT|LCD|AntiGlare|Anti-Glare|Anti Glare)/i

Well, you can not modify those regex, but I suppose you can modify the text. 好吧,您不能修改那些正则表达式,但是我想您可以修改文本。 So, you can do this. 因此,您可以执行此操作。

Use regex /antiglare/ with case insensitive flag and then replace them with ANTI-GLARE . 使用不区分大小写的regex /antiglare/标志,然后将其替换为ANTI-GLARE I hope this will work for you. 希望这对您有用。 I can't give you code, as I do not myself know php. 我不能给你代码,因为我自己也不知道php。

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

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