Java:如何使用正则表达式将字符串分成多个部分? [英] Java: how to separate string into parts using regex?

查看:329
本文介绍了Java:如何使用正则表达式将字符串分成多个部分?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须将Java字符串解析为3种单独的情况:

I have to parse a Java String into 3 separate cases:

  1. 如果它的格式为"PREFIX(<signed_float>)=<Some_alpha_num_string>",我需要将<signed_float>提取到一个(Double)变量中,将<Some_alpha_num_string>提取到另一个(String)变量中,然后忽略其余的变量.
  2. 否则,如果格式为"PREFIX=<Some_alpha_num_string>",则保存<Some_alpha_num_string>并将Double设置为默认值(例如0.0)
  3. 否则我什么都不做
  1. If it has the form "PREFIX(<signed_float>)=<Some_alpha_num_string>", I need to extract <signed_float> into one (Double) variable, <Some_alpha_num_string> into another (String) variable and ignore the rest.
  2. Otherwise, if it has the form "PREFIX=<Some_alpha_num_string>", I save <Some_alpha_num_string> and set the Double to some default (say 0.0)
  3. Otherwise I do nothing

所以我猜#1和#2的正则表达式是PREFIX[\(]?[-]?[0-9]*\.?[0-9]*[\)]?=\S*,但是我怎么用它来提取两部分呢?

So I guess the regex for #1 and #2 would be PREFIX[\(]?[-]?[0-9]*\.?[0-9]*[\)]?=\S*, but how do I use it to extract the two pieces?

顺便说一句,我不必担心以科学("%e")表示法来表示浮点数

BTW, I don't need to worry about the float being expressed in the scientific ("%e") notation

更新:需要澄清的一点:PREFIX是固定字符串.因此,有效字符串的示例为:

UPDATE: A bit of clarification: PREFIX is a fixed string. So examples of valid strings would be:

  • PREFIX=fOo1234bar-在这里我需要提取fOo1234bar
  • PREFIX(-1.23456)=SomeString-在这里我需要提取-1.23456SomeString
  • PREFIX(0.20)=1A2b3C-在这里我需要提取0.201A2b3C
  • PREFIX=fOo1234bar -- here I need to extract fOo1234bar
  • PREFIX(-1.23456)=SomeString -- here I need to extract -1.23456 and SomeString
  • PREFIX(0.20)=1A2b3C -- here I need to extract 0.20 and 1A2b3C

推荐答案

鉴于您的正则表达式,我认为<signed_float>不支持科学表示法.

Given your regex, I'll assume that <signed_float> does not support scientific notation.

正则表达式,用于将浮点数/双精度数与

Regex for matching a float/double to listed in the javadoc for Double.valueOf(String).

在这种情况下, regex 将是:

PREFIX           Matching exact letters "PREFIX"
(?:              Start optional section
  \(              Matching exact character "("
  (               Start content capture #1 <signed_float>
    [+-]?          Matches optional sign
    (?:            Start choice section
      \d+\.?\d*     Matches <digits> ["."] [<digits>]
    |              Choice separator
      \.\d+         Matches "." <digits>
    )              End choice section
  )               End content capture #1
  \)              Matching exact character ")"
)?               End optional section
=                Matching exact character "="
(\S*)            Capture #2 <Some_alpha_num_string>

或作为字符串:

"PREFIX(?:\\(([+-]?(?:\\d+\\.?\\d*|\\.\\d+))\\))?=(\\S*)"

让我们对其进行测试:

public static void main(String[] args) {
    test("PREFIX=fOo1234bar");
    test("PREFIX(-1.23456)=SomeString");
    test("PREFIX(0.20)=1A2b3C");
    test("sadfsahlhjladf");
}
private static void test(String text) {
    Pattern p = Pattern.compile("PREFIX(?:\\(([+-]?(?:\\d+\\.?\\d*|\\.\\d+))\\))?=(\\S*)");
    Matcher m = p.matcher(text);
    if (! m.matches())
        System.out.println("<do nothing>");
    else if (m.group(1) == null)
        System.out.println("'" + m.group(2) + "'");
    else
        System.out.println(Double.parseDouble(m.group(1)) + ", '" + m.group(2) + "'");
}

输出:

'fOo1234bar'
-1.23456, 'SomeString'
0.2, '1A2b3C'
<do nothing>

这篇关于Java:如何使用正则表达式将字符串分成多个部分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆