I have a series of strings and I need to extract a certain portion of them. I would like to use regular expressions to do this. This strings all have this general form:
content/landdata/files/Albuquerque_123.zip
Most of the string is static except for the city name. It could be any city name.
I've gotten as far as this:
(?!content/landdata/files/)(?:[A-Za-z_-])*[_][0-9]{1,}(?!\\.zip)
Which will give me Albuquerque_123
but I'm having trouble getting rid of the _123
.
You can use Match.Groups property.
For example:
var testString = "content/landdata/files/Albuquerque_123.zip";
var regex = new Regex(@"content/landdata/files/([A-Za-z_-]+)_[0-9]+\.zip");
Console.WriteLine(regex.Match(testString).Groups[1]);
?:
means non-capturing group so get rid of it in the group you actually want to capture.
(?!content/landdata/files/)(?:[A-Za-z_-])*[_][0-9]{1,}(?!\\.zip)
should be
(?!content/landdata/files/)([A-Za-z_-]*)_[0-9]+(?!\\.zip)
... and then you can just query the Groups
property of the match.
Could you give a bit more context in regards to "getting rid of the _123?"
My understanding of what you are asking for is as follows: You are essentially just trying to remove the _123 from the regex'd string so it becomes /content/landdata/files/[City Name]
So if this is the case, perform a substitution on the _123.
public class Example
{
public static void Main()
{
string pattern = "_123";
string output = Regex.Replace(input, pattern, "");
Console.WriteLine("Output: " + result);
}
}
I didn't really test the above code, and there may be issues with it. There is probably also a better way to do this.
这对我有用-将城市放入捕获组中(删除了?:,并从您所在的组中移动了*)
content\/landdata\/files\/([A-Za-z_-]*)[_][0-9]{1,}(?!\\.zip)
It looks like you are almost there. Consider the following regex...
(?!content/landdata/files/)(?:[A-Za-z_-])+(?=[_][0-9]{1,}\.zip)
Good Luck!
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.