正则表达式或 Java 将某些区域转换为较低的字符串

发布于 2024-12-14 01:52:31 字数 224 浏览 3 评论 0原文

我有一段正则表达式 "([AZ]|_)*" 来查找 xml 文档中属性的大写值。我想把所有的发现都变成小写。我知道我可以使用 $ 符号引用找到的组,但是如何将 $ 变量的值传递到 toLowerCase()功能。我不在乎解决方案是否涉及 java 或者简单的文本编辑器是否可以解决问题,因为这是一次性工作。

谢谢。

I have got a piece of regex "([A-Z]|_)*" to find upper case values of attributes in a xml document. I would like to turn all the findings into lower case. I know I can reference the found group with the $ sign, but how can I pass the value of the $variable into a toLowerCase() function. I don't care if the solution involves java or if a simple text editor would do the trick, because it's a one time jobs.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

帅气尐潴 2024-12-21 01:52:31

首先,对于这个特定的任务,您可能应该使用 XML 解析器。话虽如此,对于正则表达式,您可以使用 Matcher 来存储捕获的组:

Pattern p = Pattern.compile("([A-Z]|_)+"); 
String s = "test=\"UPPERCASE\" other=\"lowercase\" map=\"UPPER\"";
Matcher m = p.matcher(s);
while (m.find()) {
  String group = m.group(0);
  System.out.println(group.toLowerCase());  // will print "uppercase" and "upper"
}

First of all, for this particular task, you should probably use an XML parser instead. Having said that, with regular expressions, you can use a Matcher to store the captured groups:

Pattern p = Pattern.compile("([A-Z]|_)+"); 
String s = "test=\"UPPERCASE\" other=\"lowercase\" map=\"UPPER\"";
Matcher m = p.matcher(s);
while (m.find()) {
  String group = m.group(0);
  System.out.println(group.toLowerCase());  // will print "uppercase" and "upper"
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文