0

I use the following regex @"^(.+)/([^/]+)$" and I need to check if its path contains alfanumeric like slash.
Currently it is working but if I put like aaa/bbb/ I got error since I have the last path after bbb. How can I solve it?

user3241019
  • 811
  • 9
  • 20
Jean Tehhe
  • 1,247
  • 5
  • 19
  • 38
  • If you capture with `.` and `[^/]`, respectively, the path will contain quite a bit more than just alphanumeric characters. – O. R. Mapper Apr 13 '14 at 16:14
  • 1
    Agreed, the `.*` is a dangerous plan. something like `(\d*|\w*)+` is really alphanumeric – abc123 Apr 13 '14 at 16:19
  • How deep can be the path? Is it always 2-level deep? If it is, then `^\w+/\w+/?$` will work for your. If it is 2-or-more-level deep, then `^(\w+/)+\w+/?$` will work for you. – Ulugbek Umirov Apr 13 '14 at 16:39
  • Check [this question](http://stackoverflow.com/questions/23045378/regex-for-path-and-alpha-numeric-character/23045688#23045688). This may help you. – Sabuj Hassan Apr 13 '14 at 17:14

1 Answers1

1

Regex

^(.*)\/([^\/]+)(\/)?$

Regular expression visualization

Debuggex Demo

Description

^ assert position at start of the string
1st Capturing group (.*)
    .* matches any character (except newline)
        Quantifier: Between zero and unlimited times, as many times as possible, giving back as needed [greedy]
    \/ matches the character / literally
2nd Capturing group ([^\/]+)
    [^\/]+ match a single character not present in the list below
        Quantifier: Between one and unlimited times, as many times as possible, giving back as needed [greedy]
    \/ matches the character / literally
3rd Capturing group (\/)?
    Quantifier: Between zero and one time, as many times as possible, giving back as needed [greedy]
    Note: A repeated capturing group will only capture the last iteration. Put a capturing group around the repeated group to capture all iterations or use a non-capturing group instead if you're not interested in the data
    \/ matches the character / literally
$ assert position at end of the string

C# Code

string pattern = "^(.+)\/([^\/]+)(\/)?$";

Alphanumeric Regex

^(\w*)\/(\w*)(\/)?$

Regular expression visualization

Debuggex Demo

abc123
  • 17,855
  • 7
  • 52
  • 82
  • Thanks ,voted up .Sorry but this is part of what I want .I need alphanumeric with slash that's it,can you help please?here you can put @ or $ and its still working... – Jean Tehhe Apr 13 '14 at 16:26
  • @abc123 `\w` already includes digits. And you don't need to escape `/`. – Ulugbek Umirov Apr 13 '14 at 17:49
  • @UlugbekUmirov updated and you do need to escape `/` unless you do `@"/";` in C# – abc123 Apr 13 '14 at 20:13
  • @abc123 Nope, you need to escape \ symbol, not `/`. – Ulugbek Umirov Apr 13 '14 at 20:22
  • @UlugbekUmirov according to http://regex101.com/r/sH2iH7 you do...perhaps not with C# – abc123 Apr 13 '14 at 20:43
  • @abc123 [What special characters must be escaped in regular expressions?](http://stackoverflow.com/a/400316/1803777) - and `^\w*/\w*/?$` works fine in C#. `Console.WriteLine(Regex.IsMatch("abc123/def456", @"^\w*/\w*/?$"));` or without `@` - `Console.WriteLine(Regex.IsMatch("abc123/def456", "^\\w*/\\w*/?$"));` <= you may notice that / is not escaped. – Ulugbek Umirov Apr 13 '14 at 20:50