Reputation: 901
Am struggling in writing the regular expression pattern for the below string. I used the below pattern to get the matched string. But, i got the error.
Note: The input string may be anyone of the below input string.
string input = "IN-7874 - hello";
// or "IN-7874 - Hello"
// "IN-7874 - 1) hello"
// "IN-7874 - 1. hello"
// "IN-7874 - 1)hello"
// "IN-7874 - 1.hello"
string pattern = @"^[A-Z]+\\-^[0-9]\s+\\-\\s+^[A-Z]"; //[any number of capital letters]hyphen[any number of numbers(0-9)]space[hyphen]space[numbers or strings]
var a = Regex.Match(input, pattern);
Could anyone please help me on this?
My output should be in the pattern form of [any number of capital letters]hyphen[any number of numbers(0-9)]space[hyphen]space
Example: {SAM-123 - }// don't consider the curly brace.
Upvotes: 1
Views: 59
Reputation: 627536
You can use
^[A-Z]+-[0-9]+\s+-\s+(?:[0-9]+[.)]\s*)?[A-Za-z]+
See the regex demo
Explanation:
^
- start of string[A-Z]+
- 1 or more uppercase ASCII letters-
- a hyphen[0-9]+
- 1 or more digits\s+
- 1+ whitespaces-
- a hyphen\s+
- see above(?:[0-9]+[.)]\s*)?
- an optional sequence of:
[0-9]+
- 1+ digits[.)]
- a literal .
or )
\s*
- 0+ whitespaces[A-Za-z]+
- 1 or more ASCII lettersUpvotes: 1