简体   繁体   English

找到没有被困在双引号java正则表达式之间的特定字符串

[英]find the particular string that not being trapped between double quotes java regex

I want to find a string (say x ) that satisfies two conditions:我想找到一个满足两个条件的字符串(比如x ):

  1. matches the pattern \\b(x)\\b匹配模式\\b(x)\\b
  2. does not match the pattern ".*?(x).*?(?<!\\\\)"与模式".*?(x).*?(?<!\\\\)"不匹配

In other words, I am looking for a value of x that is a complete word (condition1) and it is not in double quotes (condition2).换句话说,我正在寻找x的值,它是一个完整的单词(条件 1)并且它不在双引号中(条件 2)。

  • " x /" m" not acceptable " x /" m"不可接受
  • " x \\" " + x + " except" :only the second x is acceptable. " x \\" " + x + " except" :只有第二个x是可以接受的。

What Java code will find x ?什么 Java 代码会找到x

The first condition is straight forward.第一个条件是直截了当的。 To check second condition you will have to check number of valid double quotes.要检查第二个条件,您必须检查有效双引号的数量。 If they are even then the string captured in first condition is valid.如果它们是偶数,则在第一个条件中捕获的字符串有效。

String text = "basdf + \" asdf \\\" b\" + b + \"wer \\\"\"";
String toCapture = "b";
Pattern pattern1 = Pattern.compile("\\b" + toCapture + "\\b");
Pattern pattern2 = Pattern.compile("(?<!\\\\)\"");
Matcher m1 = pattern1.matcher(text);
Matcher m2; 
while(m1.find()){                               // if any <toCapture> found (first condition fulfilled)
    int start = m1.start();
    m2 = pattern2.matcher(text);
    int count = 0;
    while(m2.find() && m2.start() < start){     // count number of valid double quotes "
        count++;
    }
    if(count % 2 == 0) {                        // if number of valid quotes is even 
        char[] tcar = new char[text.length()];
        Arrays.fill(tcar, '-');
        tcar[start] = '^';
        System.out.println(start);
        System.out.println(text);
        System.out.println(new String(tcar));
    }
}

Output :输出 :

23
basdf + " asdf \" b" + b + "wer \""
-----------------------^-----------

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

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