Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/371.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/20.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 - Fatal编程技术网

Java 如何将模式文本替换为其他包含基本文本元素的文本

Java 如何将模式文本替换为其他包含基本文本元素的文本,java,regex,Java,Regex,我想将一些文本替换为包含基本文本元素的其他文本 例如: text text blabla HYPERLINK "mailto:x@x.com"x@x.com text text 我想替换HYPERLINK“mailto:x@x.com"x@x.com至 因此,结果应该是: text text blabla <a href="x@x.com">x@x.com</a> text text 文本 布拉布拉 文本 如何使用Java实现这一点?以下是实现这一点的方法: Str

我想将一些文本替换为包含基本文本元素的其他文本

例如:

text text
blabla HYPERLINK "mailto:x@x.com"x@x.com
text text
我想替换
HYPERLINK“mailto:x@x.com"x@x.com至

因此,结果应该是:

text text
blabla <a href="x@x.com">x@x.com</a>
text text
文本
布拉布拉
文本

如何使用Java实现这一点?

以下是实现这一点的方法:

String str = "text text\n" +
    "blabla HYPERLINK \"mailto:x@x.com\"x@x.com\n" +
    "text text";

str = str.replaceAll("HYPERLINK \\\"mailto:(.*?)\\\"\\1", "<a href=\"$1\">$1</a>");     
System.out.println(str);
String str=“text-text\n”+
“blabla超链接\”邮件收件人:x@x.com\"x@x.com\n“+
“文本”;
str=str.replaceAll(“HYPERLINK\\\”邮件收件人:(.*?\\\“\\1”,”);
系统输出打印项次(str);
编辑:

您可以从SO此处使用的超链接标记中获取线索,并执行以下操作以获得更通用的解决方案:

String str = 
    "text text\n" +
    "blabla (mailto:x@x.com)[this email] or (mailto:x@y.com)[x@y.com]\n" +
    "text (http://www.google.com/)[this is google] text";

str = str.replaceAll("\\((.*?)\\)\\[(.*?)\\]", "<a href=\"$1\">$2</a>");       
System.out.println(str);
String str=
“文本\n”+
“布拉布拉(邮寄:x@x.com)[此电子邮件]或(邮寄至:x@y.com)[x@y.com]\n“+
“文本(http://www.google.com/)[这是谷歌]文本”;
str=str.replaceAll(“\\(.*?\)\\[(.*?\\]),”;
系统输出打印项次(str);

以下是您可以做到这一点的方法:

String str = "text text\n" +
    "blabla HYPERLINK \"mailto:x@x.com\"x@x.com\n" +
    "text text";

str = str.replaceAll("HYPERLINK \\\"mailto:(.*?)\\\"\\1", "<a href=\"$1\">$1</a>");     
System.out.println(str);
String str=“text-text\n”+
“blabla超链接\”邮件收件人:x@x.com\"x@x.com\n“+
“文本”;
str=str.replaceAll(“HYPERLINK\\\”邮件收件人:(.*?\\\“\\1”,”);
系统输出打印项次(str);
编辑:

您可以从SO此处使用的超链接标记中获取线索,并执行以下操作以获得更通用的解决方案:

String str = 
    "text text\n" +
    "blabla (mailto:x@x.com)[this email] or (mailto:x@y.com)[x@y.com]\n" +
    "text (http://www.google.com/)[this is google] text";

str = str.replaceAll("\\((.*?)\\)\\[(.*?)\\]", "<a href=\"$1\">$2</a>");       
System.out.println(str);
String str=
“文本\n”+
“布拉布拉(邮寄:x@x.com)[此电子邮件]或(邮寄至:x@y.com)[x@y.com]\n“+
“文本(http://www.google.com/)[这是谷歌]文本”;
str=str.replaceAll(“\\(.*?\)\\[(.*?\\]),”;
系统输出打印项次(str);
这就成功了。。。(很好的挑战:-)

publicstaticvoidparse(){
Pattern p=Pattern.compile((*)超链接\“mailto:(.*)\”(\\S*)(.*));
Matcher m=p.Matcher(“blabla超链接\”邮件收件人:x@x.com\"x@x.com");
如果(m.matches()){
处理的字符串=m.group(1)+“”+m.group(4);
系统输出打印项次(已处理);
}
}
这就成功了。。。(很好的挑战:-)

publicstaticvoidparse(){
Pattern p=Pattern.compile((*)超链接\“mailto:(.*)\”(\\S*)(.*));
Matcher m=p.Matcher(“blabla超链接\”邮件收件人:x@x.com\"x@x.com");
如果(m.matches()){
处理的字符串=m.group(1)+“”+m.group(4);
系统输出打印项次(已处理);
}
}

如果我想匹配模式,结果是或。我尝试了这个方法:HYPERLINK\“[mailto:][*?(.*)\”\\1,它不适用于“”或任何url,因此您希望能够将
HYPERLINK“url”链接名称解析为
?我认为您遇到的问题是
link\u name
中的空格,例如,如果我有
HYPERLINK“www.google.com”,这是谷歌,但不是这个
(你怎么知道
这是Google
是链接的一部分,但
但不是这个
不是?这是一个很好的线索。但现在我有一个基本的问题,就是url的一般模式有“mailto:”和没有“mailto:”,以及如何在这两种情况下提取“url”和“link_name”作为文本“asd HYPERLINK\”mailto:xxx@x.com\“一些电子邮件BBB超链接\”\当我使用basicText.replaceAll(“HYPERLINK\\\”(.*?\\\”(.*?)“(.*?”,”)时,“some_page dfgdfg”;现在$2是在标记后添加的,而不是在标记前添加的:asd some_email bbbbbbb some_page“rel=”nofollow noreferrer“>asd.com“>some_page dfgdfg。这很奇怪。你能再帮我一次吗?我尝试了,但我不知道如何修复它。问题是你的第二组
(*)
不匹配任何字符,因此$2为空,并且some\u页面等不被视为匹配的一部分。您需要确定一条规则,该规则可以分隔some\u页面的哪些部分。例如,在上面,您可以确定链接不应包含空格,因此您可以执行
(\\\s+)
(一个或多个非whatspace字符)对于您的第二组。如果我想匹配模式,结果是有或,该怎么办?我尝试这个:HYPERLINK\“[mailto:*?(.*?)\”\\1,它不适用于“”或任何url,因此您需要可以解析
HYPERLINK“url”的内容将\u name
链接到
中?我认为您遇到的问题是
链接\u name
中的空格,例如,如果我有
超链接“www.google.com”,这是谷歌,但不是这个
(你怎么知道
这是Google
是链接的一部分,但
但不是这个
不是?这是一个很好的线索。但现在我有一个基本的问题,就是url的一般模式有“mailto:”和没有“mailto:”,以及如何在这两种情况下提取“url”和“link_name”作为文本“asd HYPERLINK\”mailto:xxx@x.com\“一些电子邮件BBB超链接\”\当我使用basicText.replaceAll(“HYPERLINK\\\”(.*?\\\”(.*?)“(.*?”,”)时,“some_page dfgdfg”;现在$2是在标记后添加的,而不是在标记前添加的:asd some_email bbbbbbb some_page“rel=”nofollow noreferrer“>asd.com“>some_page dfgdfg。这很奇怪。你能再帮我一次吗?我尝试了,但我不知道如何修复它。问题是你的第二组
(*)
不匹配任何字符,因此$2为空,并且some\u页面等不被视为匹配的一部分。您需要确定一条规则,该规则可以分隔some\u页面的哪些部分。例如,在上面,您可以确定链接不应包含空格,因此您可以执行
(\\\s+)
(一个或多个非whatspace字符)第二组。