Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jquery-ui/2.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# C正则表达式替换_C#_Regex - Fatal编程技术网

C# C正则表达式替换

C# C正则表达式替换,c#,regex,C#,Regex,这是我在richtextbox1中的一组字符串 /Category/5 /Category/4 /Category/19 /Category/22 /Category/26 /Category/27 /Category/24 /Category/3 /Category/1 /Category/15 http://example.org/Category/15/noneedtoadd 我想用一些url来更改所有的开始/结束 输出: http://example.com/Category/5 ht

这是我在richtextbox1中的一组字符串

/Category/5
/Category/4
/Category/19
/Category/22
/Category/26
/Category/27
/Category/24
/Category/3
/Category/1
/Category/15
http://example.org/Category/15/noneedtoadd
我想用一些url来更改所有的开始/结束

输出:

http://example.com/Category/5
http://example.com/Category/4
http://example.com/Category/19
http://example.com/Category/22
http://example.com/Category/26
http://example.com/Category/27
http://example.com/Category/24
http://example.com/Category/3
http://example.com/Category/1
http://example.com/Category/15
http://example.org/Category/15/noneedtoadd

只是问,这是什么模式

这里不需要正则表达式。遍历列表中的项目,并使用String.Format构建所需的URL

String.Format(@"http://example.com{0}", str);
如果要在字符串前检查该文本框中的某个项是否为完整格式的URL,请使用string.StartsWith


因为您正在处理URI,所以可以利用它来解析相对URI:

Uri baseUri = new Uri("http://example.com/");

Uri result1 = new Uri(baseUri, "/Category/5");
// result1 == {http://example.com/Category/5}

Uri result2 = new Uri(baseUri, "http://example.org/Category/15/noneedtoadd");
// result2 == {http://example.org/Category/15/noneedtoadd}
原始正则表达式模式是^/这意味着它将匹配行首的斜杠

Regex.Replace (text, @"^/", "http://example.com/")

如果在richtextbox中,有一个字符串不是以/?开头的,所以它是/category/1或category/1?为什么在需要螺丝刀时使用冲击扳手?如果我添加了多行呢?我不明白你说的是什么?m。你是说RegexOptions.MultiLine吗?如果是这样的话,那对我的正则表达式来说并不重要。
Regex.Replace (text, @"^/", "http://example.com/")