Hi,
I want .net regular expression for below thing:
user can enter value tcm:12312312-221231323 or tcm:23121231-23423423-34234234
all the entry except "tcm" will be numeric.
Please help!
Hi,
I want .net regular expression for below thing:
user can enter value tcm:12312312-221231323 or tcm:23121231-23423423-34234234
all the entry except "tcm" will be numeric.
Please help!
^tcm:\d{8}-(\d{9}|\d{8}-\d{8})$
That's either tcm:(eight digits)-(nine digits) or tcm:(eight digits)-(eight digits)-(eight digits)
^tcm:\d+(-\d+){1,2}$
If you're looking for either tcm:(some digits)-(some digits) or tcm:(some digits)-(some digits)-(some digits)
Try this one, which requires two groups or three groups
tcm:\d+-\d+(-\d+)?
If there are restrictions on the lenht of the numbers, try something like:
tcm:\d{4,8}-\d{4,8}(-\d{4,8})?
(where 4 and 8 is the minimum and maximum for each group)
Well, a dash in the middle of a number is not numeric... ;)
Here are some options:
If the dashes are optional:
^tcm:[\d\-]+$
If the dashes are optional, but may not occur first or last:
^tcm:\d[\d\-]*\d$
If at least one dash is required:
^tcm:\d*-[\d\-]+$
If at least one dash is required, but may not occur first or last:
^tcm:\d+-[\d\-]*\d$
If dashes are optional, but may not occur first or last or next to each other:
^tcm:\d+(-\d+)*$
I's not clear if you want to validate the input and just match the input or if you want to extract the data.
If you just need to match the input to validate it, then:
^tcm:\d+(-\d+){1,2}$
Will only match if there are 2 or 3 groups of digits, no less, no more.
If you need to account for whitespace that may occur, you could modify the Regex like this:
^tcm\s*:\s*\d+\s*(-\s*\d+){1,2}\s*$
If you wanted to extract each set of digits:
In Perl, you would use the @result
array that would contain 2 or 3 groups of digits only if the whole pattern matched (the subject
string contains the line you want to extract data from).
my @result = $subject =~ /^tcm:(\d+)-(\d+)(?:-(\d+))?$/;
In C# you could do the equivalent thing:
MatchCollection results = null;
Regex r = new Regex(@"^tcm:(\d+)-(\d+)(?:-(\d+))?$");
results = r.Matches(subject);
if ((results.Count == 2) || ((results.Count == 3))) {
// use results.Item[] to access each group of digits
} else {
// The subject doesn't match
}