简体   繁体   English

Java Regex:替换为捕获组

[英]Java Regex: replace with capturing group

I have a string like this: 我有一个像这样的字符串:

123456-1/1234/189928/2323 (102457921)

I want to get 102457921 . 我想得到102457921 How can I achieve it with regex? 我如何使用正则表达式实现它?

I have tried: 我努力了:

"123456-1/1234/189928/2323 (102457921)".replaceAll("(\\.*\()(\d+)(\))","$2");

But it does not work. 但这行不通。 Any hints? 有什么提示吗?

怎么样

"123456-1/1234/189928/2323 (102457921)".replaceAll(".*?\\((.*?)\\).*", "$1");

好吧,你可以这样做:

"123456-1/1234/189928/2323 (102457921)".replaceAll(".*\((.+)\)","$1");

您可以这样做:

"123456-1/1234/189928/2323 (102457921)".replaceAll(".*?\(([^)]+)\)","$1");

怎么样“双”替换所有正则表达式简化了一个

"123456-1/1234/189928/2323 (102457921)".replaceAll(".*\\(", "").replaceAll("\\).*", "");

You can try something like that: 您可以尝试类似的方法:

var str = '123456-1/1234/189928/2323 (102457921)';
    console.log(str.replace(/[-\d\/ ]*\((\d+)\)/, "$1"));
    console.log((str.split('('))[1].slice(0, -1));
    console.log((str.split(/\(/))[1].replace(/(\d+)\)/, "$1"));
    console.log((str.split(/\(/))[1].substr(-str.length - 1, 9));
    console.log(str.substring(str.indexOf('(') + 1, str.indexOf(')')));

In other case you must be familiar with the specifics of the input data to generate a suitable regexp. 在其他情况下,您必须熟悉输入数据的细节才能生成合适的正则表达式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM