Home > other >  Regex Help needed - number string pattern
Regex Help needed - number string pattern

Time:04-10

I must create a Regex Pattern to check if the number string matches the following rules:

-must only contain numbers -must have a length of 13 numbers -it can only start with 1 or 2 -must not be empty

The strings look like this: 192031933667787 19203193326677 192031933667Z 1920319336677 09203193366778 192-3193326677 192o31933667Z 1920319336677 21314124124412

The ideal string should look like this: 1960726125938

CodePudding user response:

This should work

/^[12]\d{12}$/

^ matches the start of the string.

[12] matches 1 or 2 as the first digit.

\d{12} matches 12 remaining digits.

$ matches the end of the string.

CodePudding user response:

Using Javascript

function lfunko() {
  const a = ["192031933667787", "19203193326677", "192031933667Z", "1920319336677", "09203193366778", "192-3193326677", "1920319336677", "21314124124412"];
  let o = a.map(e => [e.match(/^[12][0-9]{12}$/)?"TRUE":"FALSE"]);
  Logger.log(JSON.stringify(o));

}


Execution log
9:49:25 AM  Notice  Execution started
9:49:25 AM  Info    [["FALSE"],["FALSE"],["FALSE"],["TRUE"],["FALSE"],["FALSE"],["TRUE"],["FALSE"]]
9:49:26 AM  Notice  Execution completed
  • Related