简体   繁体   English

字符串末尾“ /”的C#正则表达式

[英]C# regular expression for /" at end of string

I have a collection of url's and i need to write regular expression to filter needed content. 我有一个url集合,我需要编写正则表达式来过滤所需的内容。

/data/43492-someText/" / data / 43492-someText /“

/data/221639-anotherText/" / data / 221639-anotherText /“

/data/116345-differentText/" / data / 116345-differentText /“

/data/6630-boooring/" / data / 6630-boooring /“

/data/220742-foo/" / data / 220742-foo /“

What i need is only strings without /" on the end, so 我需要的只是结尾没有/的字符串,所以

/data/220742-foo / data / 220742-foo

My Regular Expression looks like this: 我的正则表达式如下所示:

@"/data/[0-9]{1,10}-.*""\s"

Note: I dont want to do this with string replace, because of some limitations on my project. 注意:由于项目上的某些限制,我不想使用字符串替换来执行此操作。

If that (string not ending in / ) is the only requirement, then use something like this: 如果唯一的要求(不以/结尾的字符串)是唯一的要求,那么请使用如下代码:

var desiredUrls = urls.Where(url => !url.EndsWith("/\""))

I initially read the question as a desire to filter urls but I can see how it could be a mapping question. 我最初将这个问题看做是出于对过滤url的渴望,但是我可以看到它可能是一个映射问题。

var withoutSuffix = urls.Select(url => url.TrimEnd("/\"".ToCharArray());

I think Regular Expressions are kind of overkill for what you're trying to do. 我认为正则表达式对于您要执行的操作有些过大。

Anyways you can use something like this : 不管怎么说,你可以使用像这样

@"/data/[0-9]{1,10}-[^/]+"

您可以使用TrimEnd从字符串末尾删除字符:

s.TrimEnd('/', '"')

You could use something like: 您可以使用类似:

(/data/[0-9]{1,10}-.+)/

And the string without the trailing / will be in the first capture group. 并且不带尾部/的字符串将在第一个捕获组中。

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

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