简体   繁体   English

正则表达式中的多个条件

[英]Multiple condition in Regex

I want to replace my variable name in text file using regular expression with following condition : let's take example that I want to replace log with LOG 我想使用具有以下条件的正则表达式替换文本文件中的变量名:让我们举一个例子,我想用LOG替换log

  1. If the line starts with import then I don't want to replace it. 如果该行以import开头,那么我不想替换它。 like import com.test.log.myfile; import com.test.log.myfile; distance between import and log can be max up to 1000 → I don't want to replace here 导入和日志之间的最大距离最大为1000→我不想在这里替换
  2. If it is contained within double quotes such as "log" then I don't want to replace it. 如果它包含在双引号中,例如“ log”,那么我不想替换它。
  3. Other than above 2 condition, I want to change it everywhere like this: 除了上述2个条件之外,我想像这样在各处进行更改:

     log="log" ; // should be LOG="log"; log.test(); // should be LOG.test(); 

I used below regular expression but it fulfilling my second and third condition but not first one: Link 我使用了以下正则表达式,但它满足了我的第二个和第三个条件,但没有满足第一个条件: 链接

(\blog\b)(?=(?:[^\"]|[\"][^\"]*[\"])*$)

can someone please help me to how to proceed or fix it ? 有人可以帮我进行或修复它吗?

You can use lookarounds to determine if it is surrounded by quotes. 您可以使用环视来确定它是否被引号引起来。

Like this (?<!")log(?!") 像这样 (?<!")log(?!")

or if lookarounds aren't supported in whatever language you're using, 或者如果您所使用的语言不支持环视,

You can use this (?:[^"]|^)(log)(?:[^"]|$) 您可以使用此 (?:[^"]|^)(log)(?:[^"]|$)

Keep in mind if you use the 2nd one, you'll want to replace capture group 1, not the whole match. 请记住,如果使用第二个,您将要替换捕获组1,而不是整个匹配项。 As you can see in my example it cuts the letter (or newline) before and after "log" if you use the full match. 如您在我的示例中看到的,如果使用完全匹配,它将在“ log”之前和之后剪切字母(或换行符)。

Given your current requirements and restrictions, you may use the following Java regex compliant pattern: 根据当前的要求和限制,您可以使用以下符合Java regex的模式:

s.replaceAll("(?<!^import\\b.{0,1000})\\b(log)\\b(?=(?:[^\"]|\"[^\"]*[\"])*$)", "LOG")

See the Java demo here . 此处查看Java演示

Details 细节

  • (?<!^import\\\\b.{0,1000}) - a negative lookbehind that fails the match if the current location is preceded with import and any 0 to 1000 symbols other than line break chars at the start of the string (or line if Pattern.MULTILINE flag is used) (?<!^import\\\\b.{0,1000}) -如果当前位置的前面是import且字符串开头的除换行符以外的任何0到1000个符号(如果使用Pattern.MULTILINE标志,则为行)
  • \\\\b(log)\\\\b - Group 1: log whole word \\\\b(log)\\\\b第1组: log整个单词
  • (?=(?:[^\\"]|\\"[^\\"]*[\\"])*$) - there should be no " chars or there may be any number of substrings inside two quotes right after log . (?=(?:[^\\"]|\\"[^\\"]*[\\"])*$)应该没有- "字符或之后可能有两个单引号内的任何数量的子串的log

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

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