Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/337.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正则表达式导出属性值_Java_Regex_Parsing - Fatal编程技术网

如何使用JAVA正则表达式导出属性值

如何使用JAVA正则表达式导出属性值,java,regex,parsing,Java,Regex,Parsing,我有这样的字串: <a href="https://host-test.com/create?userName=test3&amp;user-mail=myemail@gmail.com&amp;id=14b72820-3855-4f2b-9a39-543ced6784a0&amp;downloadurl=https://host-test.com:443/123/rest/tmp-z7vvymo3wmfzke/vfs/v2/downloadzip/&amp

我有这样的字串:

<a href="https://host-test.com/create?userName=test3&amp;user-mail=myemail@gmail.com&amp;id=14b72820-3855-4f2b-9a39-543ced6784a0&amp;downloadurl=https://host-test.com:443/123/rest/tmp-z7vvymo3wmfzke/vfs/v2/downloadzip/&amp;projectid=d29ya3NwYWNleXFpYXlwZjgwb2sxNDA2MjovY3JlYXRlQWNj:createAcc;" style="font-family:Myriad Pro,arial,tahoma,serif;color:#fff;font-size:14px;text-decoration:none;font-weight:bold" title="Confirm tenant creation" target="_blank">
                            <div style="font-family:'Lucida Grande',sans-serif;border-radius:5px;width:120px;min-height:40px;line-height:40px;border:1px solid #577e15;color:#fff;text-align:center;background:#e77431;margin:15px 0 15px">
                                Confirm
                            </div>
                        </a>

此外,每次的href值可以是不同的更短或更长

myString.replaceFirst(myString,^对于此特定字符串,您可以尝试以下操作

myString.replaceFirst(myString, "^<a\\s+href\\s*=\\s*\"([^\"]+)\".*", , "$1");
Pattern pattern = Pattern.compile("<a\\shref=\"([^\"]+)");
//or if you cant use group numbers use look-behind mechanism like
//Pattern.compile("(?<=<a\\shref=\")[^\"]+");
Matcher matcher = pattern.matcher(yourString);
if (matcher.find())
    System.out.println(matcher.group(1));

Pattern=Pattern.compile(“
我只需要使用regexp进行提取
只是为了确保,你不能使用任何解析器?我认为你不能用RegEx解析HTML,否则你会释放地狱或者其他东西。不,我知道如何使用解析器进行提取。@MikeChristensen我更喜欢。你可以使用RegEx并捕获该模式。
]*\shref=[''”(.['']
您的正则表达式将为例如
给出错误的结果。然后它将捕获
a“id=“y
。对不起,我错过了字符串中的附加标记,我的错。
Pattern pattern = Pattern.compile("<a\\shref=\"([^\"]+)");
//or if you cant use group numbers use look-behind mechanism like
//Pattern.compile("(?<=<a\\shref=\")[^\"]+");
Matcher matcher = pattern.matcher(yourString);
if (matcher.find())
    System.out.println(matcher.group(1));