正则表达式和相对文件路径

时间:2022-11-02 21:45:40

I am trying to match the folder name in a relative path using C#. I am using the expression: "/(.*)?/" and reversing the matching from left to right to right to left. When I pass "images/gringo/" into the regular expression, it correctly gives me "gringo" in the first group - I'm only interested in what is between the brackets. When I pass in "images/", it fails to pick up "images". I have tried using [/^] and [/$] but neither work.

我试图使用C#匹配相对路径中的文件夹名称。我使用表达式:“/(.*)?/”并从左到右,从右到左反转匹配。当我将“images / gringo /”传递给正则表达式时,它在第一组中正确地给了我“gringo” - 我只对括号之间的内容感兴趣。当我传入“images /”时,它无法获取“图像”。我尝试使用[/ ^]和[/ $],但都没有工作。

Thanks, David

4 个解决方案

#1


13  

You're probably better off using the System.IO.DirectoryInfo class to interpret your relative path. You can then pick off folder or file names using its members:

您可能最好使用System.IO.DirectoryInfo类来解释您的相对路径。然后,您可以使用其成员选择文件夹或文件名:

DirectoryInfo di = new DirectoryInfo("images/gringo/");
Console.Out.WriteLine(di.Name);

This will be much safer than any regexps you could use.

这比你可以使用的任何regexp都安全得多。

#2


3  

Don't do this. Use System.IO.Path to break apart path parts and then compare them.

不要这样做。使用System.IO.Path拆分路径部分,然后比较它们。

#3


2  

How about:

"([^/]+)/?$"
  • 1 or more non / characters
  • 1个或多个非/字符

  • Optional /
  • End of string
  • 字符串结束

But as @Blair Conrad says - better to go with a class that encapsulates this for you....

但正如@Blair Conrad所说的那样 - 最好还是选择一个为你封装这个的类......

#4


1  

Agreed with the "don't do it this way" answers, but, since it's tagged "regex"...

同意“不要这样做”的答案,但是,因为它被标记为“正则表达式”......

  • You don't need the ?. * already accepts 0 repetitions as a match, so (.*) is exactly equivalent to (.*)?
  • 你不需要? *已经接受0次重复作为匹配,所以(。*)完全等同于(。*)?

  • You rarely actually want to use .* anyhow. If you're trying to capture what's between a pair of slashes, use /([^/]*)/ or else testing against "foo/bar/baz/" will (on most regex implementations) return a single match for "bar/baz" instead of matching "bar" and "baz" separately.
  • 你总是很少想要使用。*无论如何。如果你试图捕捉一对斜杠之间的内容,使用/([^ /] *)/或者对“foo / bar / baz /”进行测试(在大多数正则表达式实现中)将为“bar”返回单个匹配/ baz“而不是分别匹配”bar“和”baz“。

#1


13  

You're probably better off using the System.IO.DirectoryInfo class to interpret your relative path. You can then pick off folder or file names using its members:

您可能最好使用System.IO.DirectoryInfo类来解释您的相对路径。然后,您可以使用其成员选择文件夹或文件名:

DirectoryInfo di = new DirectoryInfo("images/gringo/");
Console.Out.WriteLine(di.Name);

This will be much safer than any regexps you could use.

这比你可以使用的任何regexp都安全得多。

#2


3  

Don't do this. Use System.IO.Path to break apart path parts and then compare them.

不要这样做。使用System.IO.Path拆分路径部分,然后比较它们。

#3


2  

How about:

"([^/]+)/?$"
  • 1 or more non / characters
  • 1个或多个非/字符

  • Optional /
  • End of string
  • 字符串结束

But as @Blair Conrad says - better to go with a class that encapsulates this for you....

但正如@Blair Conrad所说的那样 - 最好还是选择一个为你封装这个的类......

#4


1  

Agreed with the "don't do it this way" answers, but, since it's tagged "regex"...

同意“不要这样做”的答案,但是,因为它被标记为“正则表达式”......

  • You don't need the ?. * already accepts 0 repetitions as a match, so (.*) is exactly equivalent to (.*)?
  • 你不需要? *已经接受0次重复作为匹配,所以(。*)完全等同于(。*)?

  • You rarely actually want to use .* anyhow. If you're trying to capture what's between a pair of slashes, use /([^/]*)/ or else testing against "foo/bar/baz/" will (on most regex implementations) return a single match for "bar/baz" instead of matching "bar" and "baz" separately.
  • 你总是很少想要使用。*无论如何。如果你试图捕捉一对斜杠之间的内容,使用/([^ /] *)/或者对“foo / bar / baz /”进行测试(在大多数正则表达式实现中)将为“bar”返回单个匹配/ baz“而不是分别匹配”bar“和”baz“。