使用正则表达式获取特定值

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

Using regex get particular value

问题

我可以使用正则表达式来提取整数形式的位置值,以下是示例Java代码:

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

public class Main {
    public static void main(String[] args) {
        String input = "2020 Aug 05 09:31:25.646515 arrisxg1v4 WPEWebProcess[22024]: [AAMP-PLAYER]NotifyBitRateChangeEvent :: bitrate:2800000 desc:BitrateChanged - Network adaptation width:1280 height:720 fps:25.000000 position:256.000000";
        String regex = "position:(\\d+)\\.\\d+"; // 正则表达式模式

        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(input);

        if (matcher.find()) {
            String positionValue = matcher.group(1); // 获取第一个捕获组(位置值)
            int positionInteger = Integer.parseInt(positionValue);
            System.out.println(positionInteger); // 输出位置值的整数形式
        }
    }
}

这段代码使用正则表达式来匹配位置值并提取整数部分,然后将其转换为整数并输出。在这个例子中,位置值为256,所以最后输出的结果是256。

英文:

I have a response like below

2020 Aug 05 09:31:25.646515 arrisxg1v4 WPEWebProcess[22024]: [AAMP-PLAYER]NotifyBitRateChangeEvent :: bitrate:2800000 desc:BitrateChanged - Network adaptation width:1280 height:720 fps:25.000000 position:256.000000

From this using regex how I can retrieve position value only as Integer. Using java code I can get it using .split method But How I can get this value 256 using Regex?

答案1

得分: 2

尝试这个一行代码:

String positionStr = str.replaceAll("(?:(?!position:).)*(?:position:(\\d+))?.*", "$1");
Integer position = positionStr.isEmpty() ? null : new Integer(positionStr);

这个正则表达式匹配整个字符串,在组1中捕获目标位置值((?:...)是一个非捕获组),然后用捕获的组替换匹配项(即所有内容)。这实际上是删除了你不想要的一切。

方便的是,因为捕获是可选的(有一个量词?),如果输入没有position:值,结果将是一个空字符串。

负向先行断言(?!position:). 防止点运行到我们的目标之外。没有负向先行断言,第一个点将消耗整个输入。

英文:

Try this one-liner:

String positionStr = str.replaceAll("(?:(?!position:).)*(?:position:(\\d+))?.*", "$1");
Integer position = positionStr.isEmpty() ? null : new Integer(positionStr);

This regex matches the whole string, capturing the target position value in group 1 ((?:...) is a non-capturing group), and replaces the match (ie everything) with the captured group. This effectively deletes everything you don't want.

Conveniently, because the capture is optional (has a quantifier of ?), if the input does not have a position: value, the result is a blank string.

The negative lookahead (?!position:). prevents the dot running past our target. Without the negative lookahead, the first dot would consume the entire input.

答案2

得分: 1

你可以尝试类似这样的代码:

Pattern pattern = Pattern.compile(".*position:(\\d+(\\.\\d+))");
Matcher matcher = pattern.matcher(input);
if (matcher.matches()) {
   String position = matcher.group(1);
}

基本上,这个表达式的意思是:

  • 接受任何字符,直到出现单词 position
  • 接受一个冒号 :
  • 接受1个或多个数字(0-9),可选地后跟一个点和1个或多个数字

然后,通过使用 matcher.group,你可以获取从左边第一个括号开始的所有内容。

英文:

You could try something like this:

Pattern pattern = Pattern.compile(".*position:(\\d+(\\.\\d+))");
Matcher matcher = pattern.matcher(input);
if (matcher.matches()) {
   String position = matcher.group(1);
}

Basically what this expression says is:

  • Accept anything up until the word position
  • Accept a colon :
  • Accept 1 or more digits (0-9), optionally followed by a dot and 1 or more digits

Then by using matcher.group you take everything between parentheses starting at the 1st parenthesis from the left.

huangapple
  • 本文由 发表于 2020年8月7日 13:38:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/63295835.html
匿名

发表评论

匿名网友

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

确定