0

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.

1
  • 1
    Is it always _123 at the end, or is it just some integer? Commented Nov 25, 2013 at 17:32

5 Answers 5

2

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]);
Sign up to request clarification or add additional context in comments.

Comments

2

?: 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.

Tested here

Comments

0

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.

2 Comments

I'm nearly sure that _123 part is not static like that.
Probably not, but as I said, I was just looking for more context.
0

This works for me - puts the city in the capturing group (removed the ?: and move the * inside the group from yours)

content\/landdata\/files\/([A-Za-z_-]*)[_][0-9]{1,}(?!\\.zip)

1 Comment

[_] could be simply _
0

It looks like you are almost there. Consider the following regex...

(?!content/landdata/files/)(?:[A-Za-z_-])+(?=[_][0-9]{1,}\.zip)

Good Luck!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.