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

时间:2017-10-26 12:49:32

标签: java regex string selenium string-matching

如何创建一个表达式来匹配一些重复的字符,然后是其他字符。

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

 //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 \n\t"+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);
}

2 个答案:

答案 0 :(得分:0)

如果你想要的是删除ProtocolB跟随\字符~,那么这个非常简单的正则表达式就可以了:

'

See for yourself.

所以你的代码是:

[\\~]+'

如果您需要处理更多字符,只需将它们添加到班级[\\〜xpathJSON.replaceAll("/[\\~]+'/", "'") ]

答案 1 :(得分:0)

这是另一个:

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

替换为:

$2$3$2

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

在Java中:

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