tags:

views:

145

answers:

5

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!

A: 

Do you mean:

tcm:[\d-]+

?

Moshe
Minimum it should be tcm:2323-32232 and maximum can be tcm:2323-2323-23223, I mean minimum tcm:0-0 and max tcm:0-0-0
MKS
tcm:somedigits-somedigits-64?
MKS
+4  A: 
^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)

Patrick McElhaney
Hi patrick, its not constant it can be any number in between, minimum it can be tcm:0-0 and max can be tcm:0-0-0
MKS
+2  A: 

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)

Rowland Shaw
what about tcm:somedigits-somedigits-64?
MKS
@Manoj in that case change the last 4,8 to 2,8 or whatever is necessary
Richard Szalay
change the last 4,8 to suit -- the 4 is the minimum number of digits, and 8 is the maximum for a group. Similarly, you could use the first option there, which has at least a single digit in each group, with the last group being optional
Rowland Shaw
A: 

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+)*$
Guffa
tcm:somedigits-somedigits-64?
MKS
What do you mean?
Guffa
A: 

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
    }
    
Renaud Bompuis