gpt4 book ai didi

java - 正则表达式:在字符串中查找和替换

转载 作者:行者123 更新时间:2023-12-01 22:49:51 24 4
gpt4 key购买 nike

我有一个字符串

String customHtml = "<html><body><iframe src=https://zarabol.rediff.com/widget/end-of-cold-war-salman-hugs-abhishek-bachchan?search=true&header=true id=rediff_zarabol_widget name=rediff_zarabol_widget scrolling=auto transparency= frameborder=0 height=500 width=100%></iframe></body></html>";

我需要用另一个字符串替换 weburl 的最后一个索引。在上面的例子中替换

end-of-cold-war-salman-hugs-abhishek-bachchan

srk-confesses-found-gauri-to-be-physically-attractive

我尝试使用 Lazy /begin.*?end/ 但失败了。任何帮助将不胜感激。提前致谢。

最佳答案

正则表达式:

(?<=\/)[^\/]*(?=\?)

Java 正则表达式:

(?<=/)[^/]*(?=\\?)

替换字符串:

srk-confesses-found-gauri-to-be-physically-attractive

DEMO

Java 代码是,

String url= "<html><body><iframe src=https://zarabol.rediff.com/widget/end-of-cold-war-salman-hugs-abhishek-bachchan?search=true&header=true id=rediff_zarabol_widget name=rediff_zarabol_widget scrolling=auto transparency= frameborder=0 height=500 width=100%></iframe></body></html>";
String m1 = url.replaceAll("(?<=\\/)[^\\/]*(?=\\?)", "srk-confesses-found-gauri-to-be-physically-attractive");
System.out.println(m1);

IDEONE

关于java - 正则表达式:在字符串中查找和替换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24881240/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com