Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/20.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
Java Regex删除名称空间并将第一个字母转换为小写_Java_Regex - Fatal编程技术网

Java Regex删除名称空间并将第一个字母转换为小写

Java Regex删除名称空间并将第一个字母转换为小写,java,regex,Java,Regex,我试图使用jxpath在生成的java对象中导航。我有一个xpath形式的导航结构 a:FirstElement/aa:SecondElement/aaa:ThirdElement 我需要得到以下格式的字符串 firstElement/secondElement/thirdElement 如何在Java中使用正则表达式实现这一点?一种可能的方法: public static void main(String[] argv) { String str = "a:FirstEleme

我试图使用jxpath在生成的java对象中导航。我有一个xpath形式的导航结构

a:FirstElement/aa:SecondElement/aaa:ThirdElement
我需要得到以下格式的字符串

firstElement/secondElement/thirdElement
如何在Java中使用正则表达式实现这一点?

一种可能的方法:

public static void main(String[] argv)
  {
    String str = "a:FirstElement/aa:SecondElement/aaa:ThirdElement";

    String[] splits = str.split("/?[a-z]*:");

    String finalStr = "";
    for (String s : splits)
    {
      if (!finalStr.isEmpty())
      {
        finalStr += "/";
      }
      finalStr += s;
    }

    System.out.println(finalStr);
  }
你可以试试这个

    Matcher m = Pattern.compile(".+?:((.+?/)|(.+?$))").matcher(s);
    StringBuffer sb = new StringBuffer();
    while(m.find()) {
        int i = sb.length();
        m.appendReplacement(sb, m.group(1));
        sb.setCharAt(i, Character.toLowerCase(sb.charAt(i)));
    }
    s = sb.toString();

为什么不简单地使用StringreplaceAllregex,其中regex=\\w+:?是否要将第一个字母转换为小写?它不区分元素名称的大小写。我想知道是否可以只使用正则表达式而不使用额外的代码。但是,我需要再次遍历修改后的字符串和camel case元素。有较短的路吗?