java中分割字符串的正则表达式

发布于 2024-11-19 14:17:56 字数 177 浏览 0 评论 0原文

我想将字符串 [AO_12345678, Real Estate] 拆分为 AO_12345678Real Estate

我如何使用正则表达式在 Java 中执行此操作?

我面临的主要问题是避免“[”和“]”
请帮忙

I want to split the string say [AO_12345678, Real Estate] into AO_12345678 and Real Estate

how can I do this in Java using regex?

main issue m facing is in avoiding "[" and "]"
please help

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

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

发布评论

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

评论(5

败给现实 2024-11-26 14:17:56

它真的必须是正则表达式吗?

如果不:

String s = "[AO_12345678, Real Estate]";
String[] split = s.substring(1, s.length()-1).split(", ");

Does it really have to be regex?

if not:

String s = "[AO_12345678, Real Estate]";
String[] split = s.substring(1, s.length()-1).split(", ");
决绝 2024-11-26 14:17:56

我会采取务实的方式:

String org = "[AO_12345678, Real Estate]";
String plain = null;
if(org.startsWith("[") {
  if(org.endsWith("]") {
    plain = org.subString(1, org.length());
  } else {
    plain = org.subString(1, org.length() + 1);
  }
}

String[] result = org.split(",");

如果字符串总是被“[]”包围,您可以直接将其子串化而不进行检查。

I'd go the pragmatic way:

String org = "[AO_12345678, Real Estate]";
String plain = null;
if(org.startsWith("[") {
  if(org.endsWith("]") {
    plain = org.subString(1, org.length());
  } else {
    plain = org.subString(1, org.length() + 1);
  }
}

String[] result = org.split(",");

If the string is always surrounded with '[]' you can just substring it without checking.

提笔书几行 2024-11-26 14:17:56

假设所有输入的格式一致,一种简单的方法是完全忽略正则表达式并将其拆分。类似下面的内容是可行的:

String[] parts = input.split(","); // parts is ["[AO_12345678", "Real Estate]"]
String firstWithoutBrace = parts[0].substring(1);
String secondWithoutBrace = parts[1].substring(0, parts[1].length() - 1);
String first = firstWithoutBrace.trim();
String second = secondWithoutBrace.trim();

当然,您可以根据需要进行定制 - 例如,您可能需要在移除大括号之前检查它们是否存在。或者您可能希望保留逗号之前的任何空格作为第一个字符串的一部分。然而,这应该为您提供根据您的具体要求进行修改的基础。

在像这样的简单情况下,我更喜欢上面这样的代码,而不是提取两个字符串的正则表达式 - 我认为前者更清晰!

One easy way, assuming the format of all your inputs is consistent, is to ignore regex altogether and just split it. Something like the following would work:

String[] parts = input.split(","); // parts is ["[AO_12345678", "Real Estate]"]
String firstWithoutBrace = parts[0].substring(1);
String secondWithoutBrace = parts[1].substring(0, parts[1].length() - 1);
String first = firstWithoutBrace.trim();
String second = secondWithoutBrace.trim();

Of course you can tailor this as you wish - you might want to check whether the braces are present before removing them, for example. Or you might want to keep any spaces before the comma as part of the first string. This should give you a basis to modify to your specific requirements however.

And in a simple case like this I'd much prefer code like the above to a regex that extracted the two strings - I consider the former much clearer!

刘备忘录 2024-11-26 14:17:56

您还可以使用 StringTokenizer。代码如下:

String str="[AO_12345678, Real Estate]"
StringTokenizer st=new StringTokenizer(str,"[],",false);
String s1 = st.nextToken();
String s2 = st.nextToken();

s1=AO_12345678

s1=Real Estate

有关 StringTokenizer 的信息,请参阅 javadocs

http://download.oracle.com/javase/1.4.2/docs/api/java/util/StringTokenizer.html

you can also use StringTokenizer. Here is the code:

String str="[AO_12345678, Real Estate]"
StringTokenizer st=new StringTokenizer(str,"[],",false);
String s1 = st.nextToken();
String s2 = st.nextToken();

s1=AO_12345678

s1=Real Estate

Refer to javadocs for reading about StringTokenizer

http://download.oracle.com/javase/1.4.2/docs/api/java/util/StringTokenizer.html

尤怨 2024-11-26 14:17:56

使用正则表达式 (RE) 捕获组的另一个选项:

private static void extract(String text) {
    Pattern pattern = Pattern.compile("\\[(.*),\\s*(.*)\\]");
    Matcher matcher = pattern.matcher(text);
    if (matcher.find()) { // or .matches for matching the whole text
        String id = matcher.group(1);
        String name = matcher.group(2);
        // do something with id and name
        System.out.printf("ID: %s%nName: %s%n", id, name);
    }
}

如果考虑速度/内存,可以将 RE 优化为(使用占有量词而不是贪婪量词)
"\\[([^,]*+),\\s*+([^\\]]*+)\\]"

Another option using regular expressions (RE) capturing groups:

private static void extract(String text) {
    Pattern pattern = Pattern.compile("\\[(.*),\\s*(.*)\\]");
    Matcher matcher = pattern.matcher(text);
    if (matcher.find()) { // or .matches for matching the whole text
        String id = matcher.group(1);
        String name = matcher.group(2);
        // do something with id and name
        System.out.printf("ID: %s%nName: %s%n", id, name);
    }
}

If speed/memory is a concern, the RE can be optimized to (using Possessive quantifiers instead of Greedy ones)
"\\[([^,]*+),\\s*+([^\\]]*+)\\]"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文