Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.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
C# 使用正则表达式提取url链接_C#_Regex - Fatal编程技术网

C# 使用正则表达式提取url链接

C# 使用正则表达式提取url链接,c#,regex,C#,Regex,如何使用Regex提取以下内容中url标记之间的所有链接: /* cyrillic-ext */ @font-face { font-family: 'Montserrat'; font-style: normal; font-weight: 400; src: local('Montserrat Regular'), local('Montserrat-Regular'), url(https://fonts.gstatic.com/s/montserrat/v12/rBHvp

如何使用
Regex
提取以下内容中url标记之间的所有链接:

/* cyrillic-ext */
@font-face {
  font-family: 'Montserrat';
  font-style: normal;
  font-weight: 400;
  src: local('Montserrat Regular'), local('Montserrat-Regular'), url(https://fonts.gstatic.com/s/montserrat/v12/rBHvpRWBkgyW99dXT88n7yEAvth_LlrfE80CYdSH47w.woff2) format('woff2');
  unicode-range: U+0460-052F, U+1C80-1C88, U+20B4, U+2DE0-2DFF, U+A640-A69F, U+FE2E-FE2F;
}
/* cyrillic */
@font-face {
  font-family: 'Montserrat';
  font-style: normal;
  font-weight: 400;
  src: local('Montserrat Regular'), local('Montserrat-Regular'), url(https://fonts.gstatic.com/s/montserrat/v12/NX1NravqaXESu9fFv7KuqiEAvth_LlrfE80CYdSH47w.woff2) format('woff2');
  unicode-range: U+0400-045F, U+0490-0491, U+04B0-04B1, U+2116;
}
/* vietnamese */
@font-face {
  font-family: 'Montserrat';
  font-style: normal;
  font-weight: 400;
  src: local('Montserrat Regular'), local('Montserrat-Regular'), url(https://fonts.gstatic.com/s/montserrat/v12/SKK6Nusyv8QPNMtI4j9J2yEAvth_LlrfE80CYdSH47w.woff2) format('woff2');
  unicode-range: U+0102-0103, U+0110-0111, U+1EA0-1EF9, U+20AB;
}
/* latin-ext */
@font-face {
  font-family: 'Montserrat';
  font-style: normal;
  font-weight: 400;
  src: local('Montserrat Regular'), local('Montserrat-Regular'), url(https://fonts.gstatic.com/s/montserrat/v12/gFXtEMCp1m_YzxsBpKl68iEAvth_LlrfE80CYdSH47w.woff2) format('woff2');
  unicode-range: U+0100-024F, U+0259, U+1E00-1EFF, U+20A0-20AB, U+20AD-20CF, U+2C60-2C7F, U+A720-A7FF;
}
/* latin */
@font-face {
  font-family: 'Montserrat';
  font-style: normal;
  font-weight: 400;
  src: local('Montserrat Regular'), local('Montserrat-Regular'), url(https://fonts.gstatic.com/s/montserrat/v12/zhcz-_WihjSQC0oHJ9TCYPk_vArhqVIZ0nv9q090hN8.woff2) format('woff2');
  unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC, U+2000-206F, U+2074, U+20AC, U+2122, U+2212, U+2215;
}
它应该返回一个链接列表:

谢谢

您可以使用以下代码:

var content = "..."; // your input here
var regex = new Regex("url\\((?<url>[^\\)]+)");
var urls = regex.Matches(content).Cast<Match>()
 .Select(m => m.Groups["url"].Value)
 .Distinct()
 .ToArray();
var content=“…”;//你的意见在这里
var regex=new regex(“url\\(?[^\\)]+)”;
var url=regex.Matches(content.Cast())
.Select(m=>m.Groups[“url”].Value)
.Distinct()
.ToArray();
正则表达式解释:

url // match "url" literaly
\( // match open brace
(?<url> // named capture group
[^\)]+ // match all chars until close brace
) // close capture group
url//以文字形式匹配“url”
\(//匹配开括号
(?//命名捕获组
[^\]+//匹配所有字符,直到关闭大括号
)//关闭捕获组
您可以使用以下代码:

var content = "..."; // your input here
var regex = new Regex("url\\((?<url>[^\\)]+)");
var urls = regex.Matches(content).Cast<Match>()
 .Select(m => m.Groups["url"].Value)
 .Distinct()
 .ToArray();
var content=“…”;//你的意见在这里
var regex=new regex(“url\\(?[^\\)]+)”;
var url=regex.Matches(content.Cast())
.Select(m=>m.Groups[“url”].Value)
.Distinct()
.ToArray();
正则表达式解释:

url // match "url" literaly
\( // match open brace
(?<url> // named capture group
[^\)]+ // match all chars until close brace
) // close capture group
url//以文字形式匹配“url”
\(//匹配开括号
(?//命名捕获组
[^\]+//匹配所有字符,直到关闭大括号
)//关闭捕获组
试试
url\(.*)
。这将捕获从开始括号到第一个结束括号的所有内容
*?
表示模式将捕获满足模式的最小文本,而不是所有文本

捕获的URL将出现在第一个捕获组中,例如:

var regex=new Regex(@"url\((.*?)\)");
var urls= ( from match in regex.Matches(input).Cast<Match>()
            select match.Groups[1].Value
          ).Distinct().ToArray();
var regex=newregex(@“url\(.*?\)”);
var url=(来自regex.Matches(input.Cast()中的match)
选择match.Groups[1]。值
).Distinct().ToArray();

var url=regex.Matches(输入)
.Cast()
.Select(match=>match.Groups[1]。值)
.Distinct()
.ToArray();
试试
url\(.*)
。这将捕获从开始括号到第一个结束括号的所有内容
*?
表示模式将捕获满足模式的最小文本,而不是所有文本

捕获的URL将出现在第一个捕获组中,例如:

var regex=new Regex(@"url\((.*?)\)");
var urls= ( from match in regex.Matches(input).Cast<Match>()
            select match.Groups[1].Value
          ).Distinct().ToArray();
var regex=newregex(@“url\(.*?\)”);
var url=(来自regex.Matches(input.Cast()中的match)
选择match.Groups[1]。值
).Distinct().ToArray();

var url=regex.Matches(输入)
.Cast()
.Select(match=>match.Groups[1]。值)
.Distinct()
.ToArray();

url\([^()]+)\
在捕获组1中。
url\([^()]+)\
在捕获组1中。谢谢。有一个单独的报价来包装url怎么样?比如url:(“…test/index.htm”)?报价是可选的,可能有或没有!谢谢有一个单独的报价来包装url怎么样?比如url:(“…test/index.htm”)?报价是可选的,可能有或没有!