修正正则表达式,使其允许可选字段。

huangapple go评论72阅读模式
英文:

Fix Regular Expression to allow optional fields

问题

一个数据行的样式如下:

    $POSL,VEL,SPL,,,4.1,0.0,4.0*12

使用以下 Java 正则表达式将第7个字段(4.1)提取到名为 `SPEED` 的字段中。

    \$POSL,VEL,SPL,,,(?<SPEED>\\d+.\\d+),.*

新数据略有变化。第4、5、6个字段现在可能包含数据:

    $POSL,VEL,SPL,a,b,c,4.0,a,b,c,d

但是,正则表达式现在返回零。注意:第4、5、6个字段可能包含字母或数字。但是,它们不会包含带引号的字符串(因此我们不需要担心带引号的逗号)。

请问是否有人可以提供修复方法?
英文:

A data-line looks like this:

$POSL,VEL,SPL,,,4.1,0.0,4.0*12

The 7th field (4.1) is extracted to the named field SPEED using this Java Regexp.

\$POSL,VEL,SPL,,,(?<SPEED>\\d+.\\d+),.*

New data has slightly changed. The fields in 4,5,6 may now contain data:

$POSL,VEL,SPL,a,b,c,4.0,a,b,c,d

But, the Regexp is now returning zero. Note: fields 4, 5, 6 may contain letters or numbers. But, they will not contain quoted Strings (so we don't need to worry about quoted commas).

Can someone offer a fix please?

答案1

得分: 1

你可以使用 \w+ 来匹配任何数字/字母,用于字段 4、5、6。

\$POSL,VEL,SPL,\\w*,\\w*,\\w*,(?<SPEED>\\d+\\.\\d+),.*

正则表达式演示


请注意,在您的帖子中,示例和正则表达式可能缺少一个逗号,以获取第七个字段中的数字。

英文:

You may use \w+ for any digit/letter, for the fields 4, 5, 6

\$POSL,VEL,SPL,\\w*,\\w*,\\w*,(?&lt;SPEED&gt;\\d+.\\d+),.*

REGEX DEMO


Note that in your post, the example and the regex may miss a comma to get the numbre as seventh field


答案2

得分: 1

你可以选择使用 ,[A-Za-z0-9]* 来重复字符 a-zA-Z 和数字。

由于第二个字符串中有 1 个逗号多,你可以将该部分设为可选。

如果你只对捕获组感兴趣,而不关心最后一部分,可以在末尾省略 .*。如果该值也可以出现在字符串末尾,可以用交替 (?:,|$) 来结束模式。

请注意在此部分 \\d+\\.\\d+ 中转义点号。

在 Java 中需要双重转义反斜杠:

String regex = "\$POSL,VEL,SPL,[A-Za-z0-9]*,[A-Za-z0-9]*,(?:[A-Za-z0-9]*,)?(?<SPEED>\\d+\\.\\d+)(?:,|$)";

正则表达式演示

英文:

You could optionally repeat chars a-zA-Z and digits using ,[A-Za-z0-9]*

As there is 1 comma more in the second string, you can make that part optional.

If you are not interested in the last part, but only in the capturing group, you can omit .* at the end. If the value can also occur at the end of the string, you can end the pattern with an alternation (?:,|$)

Note to escape the dot in this part \\d+\\.\\d+

$POSL,VEL,SPL,[A-Za-z0-9]*,[A-Za-z0-9]*,(?:[A-Za-z0-9]*,)?(?&lt;SPEED&gt;\d+\.\d+)(?:,|$)

In Java with double escaped backslashes

String regex = &quot;\$POSL,VEL,SPL,[A-Za-z0-9]*,[A-Za-z0-9]*,(?:[A-Za-z0-9]*,)?(?&lt;SPEED&gt;\\d+\\.\\d+)(?:,|$)&quot;;

Regex demo

答案3

得分: 0

假设在第一个输入中缺少一个逗号

package arraysAndStrings;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegexGroupCapture {
    public static void main(String[] args) {
        String inputArr[] = { "$POSL,VEL,SPL,,,,4.1,0.0,4.0*12",
                "$POSL,VEL,SPL,a,b,c,4.0,a,b,c,d" };
        for (String input : inputArr) {
            System.out.println(extractSpeed(input));
        }
    }

    private static float extractSpeed(String input) {
        float speed = 0;
        try {
            String regex = "\$POSL,VEL,SPL,.*?,.*?,.*?,(?<SPEED>\\d+\\.\\d+),.*";
            Pattern pattern = Pattern.compile(regex);
            Matcher matcher = pattern.matcher(input);
            if (matcher.find()) {
                speed = Float.parseFloat(matcher.group(1));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return speed;
    }
}
    
输出
=====
4.1
4.0
英文:

Assuming in first input one , was missing.

    package arraysAndStrings;
    
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    
    public class RegexGroupCapture {
    	public static void main(String[] args) {
    		String inputArr[] = { &quot;$POSL,VEL,SPL,,,,4.1,0.0,4.0*12&quot;,
    				&quot;$POSL,VEL,SPL,a,b,c,4.0,a,b,c,d&quot; };
    		for (String input : inputArr) {
    			System.out.println(extractSpeed(input));
    		}
    	}
    
    	private static float extractSpeed(String input) {
    		float speed = 0;
    		try {
    			String regex = &quot;\$POSL,VEL,SPL,.*?,.*?,.*?,(?&lt;SPEED&gt;\\d+.\\d+),.*&quot;;
    			Pattern pattern = Pattern.compile(regex);
    			Matcher matcher = pattern.matcher(input);
    			if (matcher.find()) {
    				speed = Float.parseFloat(matcher.group(1));
    			}
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
    		return speed;
    	}
    }

Output
=====
4.1
4.0

huangapple
  • 本文由 发表于 2020年9月9日 00:08:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/63797698.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定