Check string based on format

I have a line that should be in the following format:

XXXX-XX-XXX-XXXX-XXXXXXXXXX-X

where X is an integer. The number of integers does not matter. I just need to make sure the line:

  • starts and ends with an integer
  • contains only integers separated by dashes

What would be the easiest way to test this?

+8
string c #
source share
4 answers

This regex should do the trick. It uses a negative lookbehind to avoid matching multiple dashes in a string.

^\d(\d|(?<!-)-)*\d$|^\d$ ^ ^ ^ ^ | | | -- is a single digit, or | | ------- ends with a digit | ----------------consists on digits or dashes not preceded by dashes ---------------------starts with a digit 

Here is the C # code that illustrates its use (also on ideone ):

 var r = new Regex("^\\d(\\d|(?<!-)-)*\\d$|^\\d$"); Console.WriteLine(r.IsMatch("1-2-3")); Console.WriteLine(r.IsMatch("1-222-3333")); Console.WriteLine(r.IsMatch("123")); Console.WriteLine(r.IsMatch("1-2-3-")); Console.WriteLine(r.IsMatch("1")); Console.WriteLine(r.IsMatch("-11-2-3-")); 
+13
source share

Use regex.

 ^\d[-0-9]+\d$ 

It is assumed that the string length is at least three characters.

Structure:

 ^ - match start of string \d - match a digit [ - start of character class containing: - - a dash 0-9 - 0 to 9 ] - end of character class + - match one or more of the previous \d - match a digit $ - match end of string 

You can change + to * to make 2-digit strings valid, and add striping to make 1-digit strings also:

 ^(\d|\d[-0-9]*\d)$ 

Note. In .NET, \d will match any Unicode digit (for example, Arabic digits will match) - if you don't want this, replace \d with [0-9] in every place.

+7
source share

you can write a regular expression that does the trick.

How can you use this regex to test your string

 ^ ---->Start of a string. $ ---->End of a string. . ----> Any character (except \n newline) {...}----> Explicit quantifier notation. [...] ---->Explicit set of characters to match. (...) ---->Logical grouping of part of an expression. * ---->0 or more of previous expression. + ---->1 or more of previous expression. ? ---->0 or 1 of previous expression; also forces minimal matching when an expression might match several strings within a search string. \ ---->Preceding one of the above, it makes it a literal instead of a special character. Preceding a special matching character, see below. \w ----> matches any word character, equivalent to [a-zA-Z0-9] \W ----> matches any non word character, equivalent to [^a-zA-Z0-9]. \s ----> matches any white space character, equivalent to [\f\n\r\v] \S----> matches any non-white space characters, equivalent to [^\f\n\r\v] \d ----> matches any decimal digits, equivalent to [0-9] \D----> matches any non-digit characters, equivalent to [^0-9] \a ----> Matches a bell (alarm) \u0007. \b ----> Matches a backspace \u0008 if in a [] character class; otherwise, see the note following this table. \t ---->Matches a tab \u0009. \r ---->Matches a carriage return \u000D. \v ---->Matches a vertical tab \u000B. \f ---->Matches a form feed \u000C. \n ---->Matches a new line \u000A. \e ---->Matches an escape \u001B $number ----> Substitutes the last substring matched by group number number (decimal). ${name} ----> Substitutes the last substring matched by a (? ) group. $$ ----> Substitutes a single "$" literal. $& ----> Substitutes a copy of the entire match itself. $` ----> Substitutes all the text of the input string before the match. $' ----> Substitutes all the text of the input string after the match. $+ ----> Substitutes the last group captured. $_ ----> Substitutes the entire input string. (?(expression)yes|no) ----> Matches yes part if expression matches and no part will be ommited. 

more information on

http://geekswithblogs.net/brcraju/archive/2003/10/23/235.aspx

+4
source share

A regular expression can probably help:

http://www.regular-expressions.info/creditcard.html

+1
source share

All Articles