简体   繁体   English

RegEx 替换 C# 中的字符串

[英]RegEx to replace string in C#

I'm really not great with RegEx in C#, never really used them but I have a long string that contains a lot of html that may contain numerous text parts like我对 C# 中的 RegEx 真的不是很好,从来没有真正使用过它们,但我有一个很长的字符串,其中包含很多可能包含许多文本部分的 html,例如

src="Folder/Uploads/fd123051-532d-4804-a0fb-fd4ce6b70f7c/cd212dd7-7600-4b3f-a7d9-9a85c85a50ca.png" src="文件夹/上传/fd123051-532d-4804-a0fb-fd4ce6b70f7c/cd212dd7-7600-4b3f-a7d9-9a85c85a50ca.png"

or或者

src="Uploads/fd123051-532d-4804-a0fb-fd4ce6b70f7c/cd212dd7-7600-4b3f-a7d9-9a85c85a50ca.png" src="上传/fd123051-532d-4804-a0fb-fd4ce6b70f7c/cd212dd7-7600-4b3f-a7d9-9a85c85a50ca.png"

I want to apply a reg ex over the string if it can be done in C# so it replaces the folder path so it will change any and all to be src = filename.extension如果可以在 C# 中完成,我想在字符串上应用一个 reg ex,这样它就会替换文件夹路径,这样它就会将任何和全部更改为 src = filename.extension

ie. IE。

src="Uploads/fd123051-532d-4804-a0fb-fd4ce6b70f7c/cd212dd7-7600-4b3f-a7d9-9a85c85a50ca.png" src="上传/fd123051-532d-4804-a0fb-fd4ce6b70f7c/cd212dd7-7600-4b3f-a7d9-9a85c85a50ca.png"

becomes变成

src="cd212dd7-7600-4b3f-a7d9-9a85c85a50ca.png" src="cd212dd7-7600-4b3f-a7d9-9a85c85a50ca.png"

Can anyone please help?有人可以帮忙吗?

RegEx for your replace:正则表达式为您替换:

src="Uploads/fd123051-532d-4804-a0fb-fd4ce6b70f7c/cd212dd7-7600-4b3f-a7d9-9a85c85a50ca.png"

Will be:将会:

F: src="(.+?)//(.+?)//(.+?).png" [You can check "Dot Matches All"] F: src="(.+?)//(.+?)//(.+?).png" [你可以勾选"Dot Matches All"]

R: src="$1/$2/$3.png"  Or you can use instead of  $1 ,   /1 /2 /3 etc.

您可以使用:

src = Path.GetFileName(src);

You need substring function that will take only the part which you want from string Please go here.您需要子字符串函数,它只会从字符串中取出您想要的部分请到这里。

Get file name from path 从路径获取文件名

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

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