Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
与Pattern.quote()相当的Java1.4.2是什么_Java_Regex_Java1.4 - Fatal编程技术网

与Pattern.quote()相当的Java1.4.2是什么

与Pattern.quote()相当的Java1.4.2是什么,java,regex,java1.4,Java,Regex,Java1.4,与Pattern.quote相当的Java1.4.2是什么 我曾在URI上使用Pattern.quote(),但现在需要使其与1.4.2兼容 好的,模式的源代码。quote可用,如下所示: public static String quote(String s) { int slashEIndex = s.indexOf("\\E"); if (slashEIndex == -1) return "\\Q" + s + "\\E"; StringBuil

与Pattern.quote相当的Java1.4.2是什么


我曾在URI上使用Pattern.quote(),但现在需要使其与1.4.2兼容

好的,
模式的源代码。quote
可用,如下所示:

public static String quote(String s) {
    int slashEIndex = s.indexOf("\\E");
    if (slashEIndex == -1)
        return "\\Q" + s + "\\E";

    StringBuilder sb = new StringBuilder(s.length() * 2);
    sb.append("\\Q");
    slashEIndex = 0;
    int current = 0;
    while ((slashEIndex = s.indexOf("\\E", current)) != -1) {
        sb.append(s.substring(current, slashEIndex));
        current = slashEIndex + 2;
        sb.append("\\E\\\\E\\Q");
    }
    sb.append(s.substring(current, s.length()));
    sb.append("\\E");
    return sb.toString();
}
基本上它依赖于

\Q  Nothing, but quotes all characters until \E
\E  Nothing, but ends quoting started by \Q

并对字符串中出现
\E
的情况进行了特殊处理。

这是引用代码:

    public static String quote(String s) {
        int slashEIndex = s.indexOf("\\E");
        if (slashEIndex == -1)
            return "\\Q" + s + "\\E";

        StringBuilder sb = new StringBuilder(s.length() * 2);
        sb.append("\\Q");
        slashEIndex = 0;
        int current = 0;
        while ((slashEIndex = s.indexOf("\\E", current)) != -1) {
            sb.append(s.substring(current, slashEIndex));
            current = slashEIndex + 2;
            sb.append("\\E\\\\E\\Q");
        }
        sb.append(s.substring(current, s.length()));
        sb.append("\\E");
        return sb.toString();
    }
似乎不难复制或由您自己或他人实施


编辑:aiobee更快,sry

以下是GNU类路径实现(以防Java许可证让您担心):

publicstaticstringquote(stringstr)
{
int eInd=str.indexOf(“\\E”);
如果(eInd<0)
{
//不需要处理反斜杠。
返回“\\Q”+str+“\\E”;
}
StringBuilder sb=新StringBuilder(str.length()+16);
sb.append(\\Q”);//开始引用
int pos=0;
做
{
//一个反斜杠被另一个反斜杠引用;
//不需要引用“E”。
sb.追加(str.substring(pos,eInd))
.append(“\\E”+“\\\”+“E”+“\\Q”);
pos=eInd+2;
}而((eInd=str.indexOf(“\\E”,pos))>=0);
sb.append(str.substring(pos,str.length()))
.append(“\\E”);//结束引号
使某人返回字符串();
}

这对我来说就足够了。请原谅我的生疏,但您是如何获得源代码的?源代码是随SDK提供的,在eclipse中,您可以按住shift键并单击某个类以查看其源代码。@Tobias,我想您的意思是按住Ctrl键并单击,但我相信这需要您指定源zip文件的位置。是的,应该是单击;)您只需要在Eclipse中将JDK用作JRE即可,您可以通过使用StringBuffer替换StringBuilder来为回复增加价值;直到JDK1.5才引入StringBuilder。
  public static String quote(String str)
  {
    int eInd = str.indexOf("\\E");
    if (eInd < 0)
      {
        // No need to handle backslashes.
        return "\\Q" + str + "\\E";
      }

    StringBuilder sb = new StringBuilder(str.length() + 16);
    sb.append("\\Q"); // start quote

    int pos = 0;
    do
      {
        // A backslash is quoted by another backslash;
        // 'E' is not needed to be quoted.
        sb.append(str.substring(pos, eInd))
          .append("\\E" + "\\\\" + "E" + "\\Q");
        pos = eInd + 2;
      } while ((eInd = str.indexOf("\\E", pos)) >= 0);

    sb.append(str.substring(pos, str.length()))
      .append("\\E"); // end quote
    return sb.toString();
  }