[英]Remove double quotes from output Java
I am trying to extract a url from the string. 我正在尝试从字符串中提取一个URL。 But I am unable to skip the double quotes in the output.
但是我无法跳过输出中的双引号。
import java.util.regex.Matcher;
import java.util.regex.Pattern;
class Main {
public static void main(String[] args) {
String s1 = "<a id=\"BUTTON_LINK\" style=\"%%BUTTON_LINK%%\" target=\"_blank\" href=\"https://||domainName||/basketReviewPageLoadAction.do\">%%CHECKOUT%%</a>";
//System.out.println(s1);
Pattern pattern = Pattern.compile("\\s*(?i)href\\s*=\\s*(\"([^\"]*\")|'[^']*'|([^'\">\\s]+))");
Matcher matcher = pattern.matcher(s1);
if(matcher.find()){
String url = matcher.group(1);
System.out.println(url);
}
}
}
My Output is: 我的输出是:
"https://||domainName||/basketReviewPageLoadAction.do"
Expected Output is: 预期输出为:
https://||domainName||/basketReviewPageLoadAction.do
I cannot do string replace. 我无法进行字符串替换。 I have add few get param in this output and attach back it to original string.
我在此输出中添加了一些get参数,并将其附加到原始字符串。
You can try one of these options: 您可以尝试以下选项之一:
System.out.println(url.replaceAll("^\"|\"$", ""));
System.out.println(url.substring(1, url.length()-1));
Regex : (?<=href=")([^\\"]*)
Substitution : $1?params...
正则表达式 :
(?<=href=")([^\\"]*)
替换 : $1?params...
Details : 详细资料 :
(?<=)
Positive Lookbehind (?<=)
正向后看 ()
Capturing group ()
捕获组 [^]
Match a single character not present in the list [^]
匹配列表中不存在的单个字符 *
Matches between zero and unlimited times *
零到无限次匹配 $1
Group 1. $1
组1。 Java code : Java代码 :
By using function replaceAll
you can add your params ?abc=12
to the end of the capturing group $1
in this case href
. 通过使用
replaceAll
函数,可以将参数?abc=12
添加到捕获组$1
的末尾(在本例中是href
。
String text = "<a id=\"BUTTON_LINK\" style=\"%%BUTTON_LINK%%\" target=\"_blank\" href=\"https://||domainName||/basketReviewPageLoadAction.do\">%%CHECKOUT%%</a>";
text = text.replaceAll("(?<=href=\")([^\"]*)", String.format("$1%s", "?abc=12"));
System.out.print(text);
Output : 输出 :
<a id="BUTTON_LINK" style="%%BUTTON_LINK%%" target="_blank" href="https://||domainName||/basketReviewPageLoadAction.do?abc=12">%%CHECKOUT%%</a>
ugly, seems works.Hope this help. 丑陋,似乎可行。希望获得帮助。
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;
class Main {
public static void main(String[] args) {
String s1 = "<a id=\"BUTTON_LINK\" style=\"%%BUTTON_LINK%%\" target=\"_blank\" href= \"https://||domainName||/basketReviewPageLoadAction.do\">%%CHECKOUT%%</a>";
//System.out.println(s1);
Pattern pattern = Pattern.compile("\\s*(?i)href\\s*=\\s*(\"([^\"]*)\"|'([^']*)'|([^'\">\\s]+))");
Matcher matcher = pattern.matcher(s1);
if (matcher.find()) {
String url = Stream.of(matcher.group(2), matcher.group(3),
matcher.group(4)).filter(s -> s != null).collect(Collectors.joining());
System.out.print(url);
}
}
}
此解决方案目前适用。
Pattern pattern = Pattern.compile("\\s*(?i)href\\s*=\\s*\"([^\"]*)");
您将尝试一下,
s1 = s1.Replace("\"", "");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.