简体   繁体   English

正则表达式选择* .aspx,直到与z有所不同

[英]Regex to select *.aspx until something different from a-z

I have something like this string: 我有类似这样的字符串:

string s = "Q.ROOT_PATH + 'pgs/ChangePassword.aspx?a=5'";

I want to get the words before .aspx until find the character different of az, Result: 我想获得.aspx之前的单词,直到找到与az不同的字符,结果:

"ChangePassword.aspx"

My Regex: 我的正则表达式:

[a-z](.aspx)

What else i need to do? 我还需要做什么?

While you could certainly use regular expression to handle this, you might consider some of the built-in functions within .NET that can already handle this for you. 尽管您当然可以使用正则表达式来处理此问题,但是您可以考虑.NET中的一些内置函数,它们已经可以为您处理此问题。 I'll provide two examples for how to resolve this (with and without regular expressions). 我将提供两个示例说明如何解决此问题(使用和不使用正则表达式)。

Using The Path.GetFileNameWithoutExtension() Method 使用Path.GetFileNameWithoutExtension()方法

The System.IO namespace actually exposes a method called GetFileNameWithoutExtension() that will handle this exact operation for you : System.IO命名空间实际上公开了一个名为GetFileNameWithoutExtension()的方法,该方法将为您处理此确切操作:

// This will find the file mentioned in the path and return it without
// an extension ("pgs/ChangePassword.aspx?a=5" > "ChangePassword")
var fileName = Path.GetFileNameWithoutExtension(input);

You can see this approach here . 您可以在这里看到这种方法

Via a Regular Expression 通过正则表达式

You can accomplish this through a lookahead which will match a string of one or more letters [a-zA-Z]+ that precede an .aspx using the Regex.Match() method: 您可以使用Regex.Match()方法通过先行匹配将匹配.aspx之前的一个或多个字母[a-zA-Z]+的字符串来完成此操作:

// This will match any set of alphabetical characters that appear before
// an '.aspx' ("pgs/ChangePassword.aspx?a=5" > "ChangePassword")
var fileName = Regex.Match(input,@"[a-zA-Z]+(?=\.aspx)");

You can see a working example here . 您可以在此处看到一个有效的示例

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

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