c# - How do i remove a variable part of a string?

2019-07-29 04:38发布

I would like to remove part of a string. It would have to match "/*/cms/" (without the quotes). * would be a wildcard equaling anything except a / key. The string could have more than one match to be removed.

This would have to be done in c#. I think this can be done with a regex? But I'm not sure.

标签: c# regex replace
4条回答
爷、活的狠高调
2楼-- · 2019-07-29 05:05
using System.Text.RegularExpressions;

Regex.Replace("/some example text/cms/;/some more text/cms/text/cms", "/[^/]+/cms/", "")

Use /[^/]+/cms/ if something must be between the first and second /'s, use /[^/]*/cms/ if //cms/ is a valid match.

查看更多
够拽才男人
3楼-- · 2019-07-29 05:14

The accepted answer in this question will help you. Ensure that you use the Regex.Replace() method to do the pattern matching and replacement.

查看更多
Viruses.
4楼-- · 2019-07-29 05:19

Look up Regex.Replace. Hacking the example quickly:

string pattern = "/[^/]+/cms/";
string replacement = "";
Regex rgx = new Regex(pattern);
string result = rgx.Replace(input, replacement);
查看更多
手持菜刀,她持情操
5楼-- · 2019-07-29 05:22

The regex to match that is /[^/]*/cms/, used like this: new Regex("/[^/]*/cms/").Replace(yourString, "")

查看更多
登录 后发表回答