Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 正则表达式从“cn=”开始提取子字符串,以第一个模式匹配{space+anyword containg“=”char}结束_Java_Regex - Fatal编程技术网

Java 正则表达式从“cn=”开始提取子字符串,以第一个模式匹配{space+anyword containg“=”char}结束

Java 正则表达式从“cn=”开始提取子字符串,以第一个模式匹配{space+anyword containg“=”char}结束,java,regex,Java,Regex,如何构建正则表达式以提取从cn=开始并以第一个模式匹配{space+anyword containg=char}结束的子字符串 例如: 输入字符串=>cn=mstero della difesa-时间戳机构eidas serial=ohj serialnumber=97355587 ou=s.m.d.-c.do c4 difesa o=mstero della difesa c=it 输出字符串=>mstero della difesa-时间戳授权eidas 我尝试了这个正则表达式->?这部分导

如何构建正则表达式以提取从cn=开始并以第一个模式匹配{space+anyword containg=char}结束的子字符串

例如: 输入字符串=>cn=mstero della difesa-时间戳机构eidas serial=ohj serialnumber=97355587 ou=s.m.d.-c.do c4 difesa o=mstero della difesa c=it

输出字符串=>mstero della difesa-时间戳授权eidas


我尝试了这个正则表达式->?这部分导致问题:*匹配到字符串的末尾。此外,该部分?=\w=不包含空格,\w表示[a-zA-Z0-9]


你可以通过积极的前瞻和积极的落后来实现这一点。您需要正则表达式:?您可以使用不情愿的量词:

cn=(.+?) \\S+=.*

你可以使用正则表达式,你能分享你目前的进展吗?请务必阅读:sure@Hulya我在这里尝试了这个正则表达式->?但我想用lazy代替请编辑您的问题,java和javascript也是不同的语言。您使用的是哪一个?但是可以用任何字符来代替[\\w\\s-]+在这之后有一个正向前瞻?=。。。因此匹配部分后面必须跟\\s\\w+=:一个空格+一个单词+=。我将添加有关正则表达式的解释…我同意,但此表达式不匹配[\\w\\s-]中包含的任何特殊字符。您可以在[..]中添加特殊字符,或使用更详细的输入和输出示例编辑问题…通常称为“非贪婪”或“懒惰”量词。javadoc提到贪婪,不情愿和占有欲。有趣的是,我从未注意到。但这在正则表达式中肯定不太常见。到目前为止,我从未使用过所有格…@Reto感谢您的回复。如果我重新调用,char也像cn=。+?[,]+\\S+=,我们可以这样做吗*
mstero della difesa - time stamp authority eidas
cn=(.+?) \\S+=.*
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
    public static void main(String[] args) {
        String text = "cn=mstero della difesa - time stamp authority eidas serial=ohj serialnumber=97355587 ou=s.m.d. - c.do c4 difesa o=mstero della difesa c=it";
        Matcher matcher = Pattern.compile("(?<=cn\\=).*?(?=\\s+\\b\\w+\\=\\w+\\b)").matcher(text);
        if (matcher.find()) {
            System.out.println(matcher.group());
        }
    }
}
mstero della difesa - time stamp authority eidas