4

I am trying to find {a number} / { a number } / {a string} patterns. I can get number / number to work, but when I add / string it does not.

Example of what I'm trying to find:

15/00969/FUL

My regex:

Regex reg = new Regex(@"\d/\d/\w");

4 Answers 4

8

You should use + quantifier that means 1 or more times and it applies to the pattern preceding the quantifier, and I would add word boundaries \b to only match whole words:

\b\d+/\d+/\w+\b

C# code (using verbatim string literal so that we just could copy/paste regular expressions from testing tools or services without having to escape backslashes):

var rx = new Regex(@"\b\d+/\d+/\w+\b");

If you want to precise the number of characters corresponding to some pattern, you can use {}s:

\b\d{2}/\d{5}/\w{3}\b

And, finally, if you have only letters in the string, you can use \p{L} (or \p{Lu} to only capture uppercase letters) shorthand class in C#:

\b\d{2}/\d{5}/\p{L}{3}\b

Sample code (also featuring capturing groups introduced with unescaped ( and )):

var rx = new Regex(@"\b(\d{2})/(\d{5})/(\p{L}{3})\b");
var res = rx.Matches("15/00969/FUL").OfType<Match>()
                       .Select(p => new
                       {
                           first_number = p.Groups[1].Value,
                           second_number = p.Groups[2].Value,
                           the_string = p.Groups[3].Value
                       }).ToList();

Output:

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

5
Regex reg = new Regex(@"\d+/\d+/\w+");

Complete example:

Regex r = new Regex(@"(\d+)/(\d+)/(\w+)");

string input = "15/00969/FUL";
var m = r.Match(input);

if (m.Success)
{
    string a = m.Groups[1].Value;   // 15
    string b = m.Groups[2].Value;   // 00969
    string c = m.Groups[3].Value;   // FUL
}

1 Comment

You should explain how this answers the question within your answer.
3

You are missing the quantifiers in your Regex

If you want to match 1 or more items you should use the +. If you already know the number of items you need to match, you can specify it using {x} or {x,y} for a range (x and y being two numbers)

So your regex would become:

Regex reg = new Regex(@"\d/+\d+/\w+");

For example if all the elements you want to match have this format ({2 digit}/{5 digit}/{3 letters}), you could write:

Regex reg = new Regex(@"\d/{2}\d{5}/\w{3}");

And that would match 15/00969/FUL

More info on the Regular Expressions can be found here

Comments

2
bool match = new Regex(@"[\d]+[/][\d]+[/][\w]+").IsMatch("15/00969/FUL"); //true

Regular Expression:

[\d]+ //one or more digits
[\w]+ //one or more alphanumeric characters
[/]   // '/'-character

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.