简体   繁体   English

C#使用正则表达式捕获第一场比赛

[英]C# Capturing the first match with regex

I've got an input string that looks like this: 我有一个看起来像这样的输入字符串:

url=https%3A%2F%2Fdomain.com%2Fsale-deal%3Futm_source%3Dinsider-primary-action%3Dinsider-primary-action&utm_source=FB

or 要么

url=https%3A%2F%2Fdomain.com%2Fsale&utm_source=FB&sub_id1=M12

the string sometimes has or non %3Futm_source 字符串有时具有或不具有%3Futm_source

how to get link between url= and %3Futm_source% or &utm_source 如何获取url =和%3Futm_source%&utm_source之间的链接

Regex reg = new Regex(@"url=(https%3A%2F%2Fdomain.com[a-zA-Z0-9-_/%\.]+)%3Futm_source|&utm_source");
                    Match result = reg.Match(inPut);
Console.WriteLine(result.Groups[1].Value));

it always get from url= to &utm_source 它总是从url=到达&utm_source

You can use this 你可以用这个

(?<=url=).*?(?=%3Futm_source|&amp;utm_source)
  • (?<=url=) Positive look behind. (?<=url=)积极地期待。 matches url= . 匹配url=
  • .* - Matches anything except new line. .* -匹配除换行以外的所有内容。
  • (?=%3Futm_source|&amp;utm_source) - Positive look ahead. (?=%3Futm_source|&amp;utm_source) -积极向前看。 Matches %3Futm_source or &amp;utm_source 匹配%3Futm_source&amp;utm_source

Demo 演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM