简体   繁体   中英

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:

"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. I'll provide two examples for how to resolve this (with and without regular expressions).

Using The Path.GetFileNameWithoutExtension() Method

The System.IO namespace actually exposes a method called GetFileNameWithoutExtension() that will handle this exact operation for you :

// 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:

// 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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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