Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.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
Regex 用正则表达式替换标记_Regex - Fatal编程技术网

Regex 用正则表达式替换标记

Regex 用正则表达式替换标记,regex,Regex,我正试着做我的家庭作业,但正则表达式对我来说是新的,我不知道为什么我的代码不能工作。这就是我要做的: 编写一个程序,用相应的标记替换作为字符串给定的HTML文档中的所有标记。读取输入,直到收到“结束”命令。在控制台上打印结果 我写道: Pattern pattern = Pattern.compile("<a href=\"(.)+\">(.)+<\\/a>"); input = input.replaceAll(matcher.toString(), "href=" +

我正试着做我的家庭作业,但正则表达式对我来说是新的,我不知道为什么我的代码不能工作。这就是我要做的:

编写一个程序,用相应的标记替换作为字符串给定的HTML文档中的所有标记
。读取输入,直到收到“结束”命令。在控制台上打印结果

我写道:

Pattern pattern = Pattern.compile("<a href=\"(.)+\">(.)+<\\/a>");
input = input.replaceAll(matcher.toString(), "href=" + matcher.group(1) + "]" + matcher.group(2) + "[/URL]");
Pattern=Pattern.compile(“

您正朝着正确的方向前进,但您不能使用这样的
模式
对象

首先,将代码更改为仅使用字符串直接使用
replaceAll()
,并在替换字符串中使用正常的反向引用
$n

这样转换的代码是:

input = input.replaceAll("<a href=(\".+\")>(.)+<\\/a>", "href=$1]$2[/URL]");
input=input.replaceAll(“,”[URL href=$1]$2[/URL]”);
更改是将
+
放在捕获组内。即
(++
->
(.+)
,并且捕获双引号,因为如果我正确解释“spec”,您必须将它们放回


还请注意,您不需要转义正斜杠。正斜杠只是所有正则表达式风格中的普通旧字符。尽管有些语言使用正斜杠来分隔正则表达式,但java不是其中之一。

您的
+
量词需要在括号内:

<a href=\"(.+)\">(.+)<\\/a>
(.+)
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Text.RegularExpressions;
使用System.Threading.Tasks;
命名空间_06.Replace_a_标记
{
班级计划
{
静态void Main(字符串[]参数)
{
string text=Console.ReadLine();
while(文本!=“结束”)
{
字符串模式=@“
//输出:
  • [URL href=”“][/URL]

对于奖励积分,您应该为您的老师提供一个链接,下次请在输入代码的同时输入简短的解释
input = input.replaceAll("<a href=(\".+\")>(.+)</a>", "[URL href=$1]$2[/URL]");
<a href=\"(.+)\">(.+)<\\/a>
 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 using System.Text.RegularExpressions;
 using System.Threading.Tasks;

 namespace _06.Replace_a_Tag
 {
    class Program
     {
       static void Main(string[] args)
        {
            string text = Console.ReadLine();
            while (text != "end")
            {
                string pattern = @"<a.*?href.*?=(.*)>(.*?)<\/a>";
                // is used to take only 2 groups :
                // first group (or group one) is used for the domain name
                // for example : "https://stackoverflow.com"

                // and the second is for if you want to enter some text 
                // (or no text)
                // for example : This is some text

                string replace = @"[URL href=$1]$2[/URL]";
                // we use $ char and a number (like placeholders)
                // for example : $1 means take whatever you find from group 1
                //        and  : $2 means take whatever you find from group 2

                string replaced = Regex.Replace(text, pattern , replace);

                //  In a specific input string (text), replaces all strings 
                //  that match a specified regular expression (pattern ) with 
                //  a specified replacement string (replace)

                Console.WriteLine(replaced);

                text = Console.ReadLine();
            }


       }
    }
  }

  //  input : <ul><li><ahref=""></a></li></ul>
  //  output: <ul><li>[URL href=""][/URL]</li></ul>