简体   繁体   English

需要更好的正则表达式来匹配文本

[英]Need a better regular expression to match a text

My requirement is to find wrong usage patterns of a method callee, for that I want to have a regular expression which can find the invalid usecases. 我的要求是找到方法被调用者的错误使用模式,为此,我想要一个可以查找无效用例的正则表达式。

Ex: I have a method, it will accepts a parameter with predefined formats. 例如:我有一个方法,它将接受具有预定义格式的参数。

Possible predefined input arguments: 可能的预定义输入参数:

  1. abc ABC
  2. bbc 英国广播公司
  3. abc bbc abc英国广播公司
  4. der DER
  5. der abc abc

Function: function method(param){} 函数: 函数方法(参数){}

Valid function callee use-cases: 有效的函数被调用者用例:

  1. object.method("abc"); 的object.method( “ABC”); or object.method('abc'); object.method('abc');
  2. object.method("bbc"); 的object.method( “BBC”); or object.method('bbc'); object.method('bbc');
  3. object.method("abc bbc"); object.method(“ abc bbc”); or object.method('abc bbc'); object.method('abc bbc');
  4. object.method("der"); 的object.method( “DER”); or object.method('der'); object.method('der');
  5. object.method("der abc"); object.method(“ der abc”); or object.method('der abc'); object.method('der abc');

Invalid use-cases: 无效的用例:

  1. object.method("abcd"); 的object.method( “ABCD”);
  2. object.method("test"); 的object.method( “测试”);
  3. object.method("abc "); object.method(“ abc”);
  4. object.method("abc bbc "); object.method(“ abc bbc”);

In order to identify the invalid use cases I have come with the below regular expression: Ref: [RegEx] ( https://regex101.com/r/fR5eF4/3 ) 为了识别无效的用例,我附带了以下正则表达式:Ref:[RegEx]( https://regex101.com/r/fR5eF4/3

Is it possible to optimize the above regular expression further (or) any other expression with better performance. 是否可以进一步优化上述正则表达式(或其他具有更好性能的其他表达式)。

If you have a closed list of valid inputs I would just check that the input coincides with one of them, for example, just by searching an array of options. 如果您有一个有效输入的封闭列表,那么我只需检查输入是否与其中一个相符即可,例如,仅搜索一组选项即可。

If that is not the case you should offer us a better explanation of the required input format. 如果不是这种情况,您应该为我们提供所需输入格式的更好解释。

i've came with 我来了

.(method)\s*\((?![\"\'](abc|bbc|abc bbc|der|der abc)[\"\'])

so i've just moved ' and " out of this (x|x|x) group, to make things clearer 所以我刚刚将'和'移出了(x | x | x)组,以使情况更清楚
also -- you can add /g modifier for testing and put all your data to textbox. 您也可以添加/ g修饰符进行测试,然后将所有数据放入文本框。
here's link: https://regex101.com/r/oT4nN3/1 这里的链接: https : //regex101.com/r/oT4nN3/1

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

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