这个正则表达式会如何呢? [英] How would that regular expression be?

查看:112
本文介绍了这个正则表达式会如何呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有如下一些规则表达式:

I have an expression that follows some rules:


  • 字符'必须是第一和最后一个字符

  • 有可能是零或更多的内

  • 有可能是零或更多的<$空间C $ C>%在

  • 有可能是零或更多的单词(字母和数字)在

  • The character ' must be first and last character
  • There can be zero-or-more spaces inside ''
  • There can be zero-or-more % inside ''
  • There can be zero-or-more words (letters and numbers) inside ''

表达式:

(?i)^(?<q>['])[%\p{Zs}\p{L}\p{N}|()]*\k<q>$

现在我需要另一个表达式全部替换)和(在TEST的字符串,例如,但只有当他们没有被包围。
诀窍是,当)或(被包围'',但这些字符属于不同。结果对'',它不应该通过

Now I need another expression to replace all ) and ( in a string for "TEST" for example, but only when they are not surrounded by ''. The trick is that when ) or ( are surrounded by '' but these characters belong to a different pair of '', it should not pass.

例如:

    '(' > pass
    ' ( ' > pass
    ')'   > pass
    ' ) '   > pass
    ' content here ' ' )'  > pass
    ' content here' ) ' another content'  > does not pass

请注意,该第一内容有它的'',和第二,以及。任何)或(不应传递如果在他们之间。

Note that the first content has its '', and the second as well. Any ) or ( should not pass if its in between them.

我不使用正则表达式一个职业球员,所以如果你不知道怎么会, 。相关的任何文件或教程会有很大的帮助。

I'm not a pro with regular expressions, so if you don't know how it would be, any documentation or tutorial related would be of great help.

推荐答案

您可以使用正则表达式:

You can use the regex:

^.*[()](?=[^']*'(?>(?:[^']*'[^']*){2})*$)

请参阅的演示

代码:

var rgxHardNut = new Regex(@"^.*[()](?=[^']*'(?>(?:[^']*'[^']*){2})*$)");
var check1 = rgxHardNut.IsMatch("'('");  // true
var check2 = rgxHardNut.IsMatch("' ( '");// true
var check3 = rgxHardNut.IsMatch("')'");  // true
var check4 = rgxHardNut.IsMatch("' ) '");// true
var check5 = rgxHardNut.IsMatch("' content here ' ' )'"); // true
var check6 = rgxHardNut.IsMatch("' content here' ) ' another content'"); // false

这篇关于这个正则表达式会如何呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆