简体   繁体   English

正则表达式中的AND

[英]AND in Regular expression

I want to write a RE in which I want to find all the select statement in the init method 我想写一个RE,我想在init方法中找到所有的select语句。

I have written it as follows but It is not working 我写了如下,但它不起作用 在此处输入图片说明

You could start off from something like so: public void init\\(\\).*?\\{.*?select\\s*\\*\\s*from (with the s flag, so that the . can match new line characters) example here . 您可以从类似这样的内容开始: public void init\\(\\).*?\\{.*?select\\s*\\*\\s*from (带有s标志,以便.可以匹配换行符) 这里的例子。

The issue with your approach is that: 您的方法存在的问题是:

  • Round brackets have a special meaning in regular expressions, so they need to be escaped with an extra \\ . 圆括号在正则表达式中具有特殊含义,因此需要使用额外的\\进行转义。
  • By default, the period character ( . ) does not match a new line, so we need to add the s flag. 默认情况下,句点字符( . )与换行符匹配,因此我们需要添加s标志。 In C#, this is available as RegexOptions.SingleLine . 在C#中,可以将其作为RegexOptions.SingleLine

PS If you want to make it more robust, you can use something like so: public\\s+void\\s+init\\s*\\(\\).*?\\{.*?select\\s*\\*\\s*from . PS如果要使其更强大,可以使用类似以下的方法: public\\s+void\\s+init\\s*\\(\\).*?\\{.*?select\\s*\\*\\s*from This would allow you some more freedom when it comes to having multiple spaces between keywords. 当关键字之间有多个空格时,这将为您提供更大的自由度。

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

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