正则表达式匹配大小写替换



如何制作一个表达式来匹配某个重复的字符,然后跟其他字符。

例如,输入字符串可能类似于以下任何字符串。

 //window[2]//header[@id='top']/div[1]//a[1]
 //window[2]//header[@id=\'top\']/div[1]//a[1]
 //window[2]//header[@id=\'top\']/div[1]//a[1]
 //window[2]//header[@id=\\'top\\']/div[1]//a[1]
 //window[2]//header[@id=\\'top\\']/div[1]//a[1]
 (OR)
 //window[2]//header[@id=~~~~~'top~~~~~']/div[1]//a[1]

并且预期的输出应如下所示。 使用ragex替换全部。

//window[2]//header[@id='top']/div[1]//a[1]

我尝试过使用这些正则表达式

xpathJSON.replaceAll("/[~{1,}[']]/", "'")
xpathJSON.replaceAll("/^[~+]&&[']$/", "'")

但没有用。

测试代码:

public static void main(String[] args) {
    String xpathJSON = "//window[2]//header[@id="top"]/div[1]//a[1]"; // « //window[2]//header[@id=\\'top\\']/div[1]//a[1]
    for (int i = 0; i < 5; i++) {
        xpathJSON = xpathJSON.replaceAll(""", "'");
        // As the windows navigation forward and backward this replace takes place.
        xpathJSON = xpathJSON.replaceAll("'", "\\'"); // ' to \'
        System.out.println("t « "+xpathJSON);
    }
    System.out.println("xapthJSON nt"+xpathJSON);
    xpathJSON = xpathJSON.replaceAll("\\", "~");
    System.out.println( xpathJSON );
    // http://www.regular-expressions.info/wordboundaries.html
    // https://docs.oracle.com/javase/8/docs/api/java/util/regex/Pattern.html
    Pattern p = Pattern.compile
            ("[~]");
            //("^[~+]&&[']$"); // ^begning +followed By $end {\\ - ~ = }
    Matcher matcher = p.matcher( xpathJSON );
    boolean match = false, find = false;
    if ( matcher.matches() ) match = true;
    if ( matcher.find() )    find  = true; // finds the next expression that matches the pattern.
    int from = 0;
    int count = 0;
    while(matcher.find(from)) {
        count++;
        from = matcher.start() + 1;
        // another approach is to break when ' index is reached.
    }
    System.out.println(count);
    System.out.format("t Match[%s] Find[%s]n", match, find);
    System.out.println("regular expression : "+ xpathJSON.replaceAll("/[~{1,}[']]/", "'"));
    while( xpathJSON.contains("~'") ) {
        xpathJSON = xpathJSON.replaceAll("~'", "'");
    }
    System.out.println("Contains Replace : "+ xpathJSON);
}

这是另一个:

([\~]+(['"]))(.*?)1

替换为:

$2$3$2

https://regex101.com/r/OFojVx/1/

在爪哇中:

.replaceAll("([\\~]+(['"]))(.*?)\1", "$2$3$2")

如果你想要的是删除字符并在它们后面跟'~,这个非常简单的正则表达式可以做到:

[\~]+'

你自己看吧。

所以你的代码将是:

xpathJSON.replaceAll("/[\~]+'/", "'")

如果需要处理更多字符,只需将它们添加到类 [\\~ insert character here ]

最新更新