Home > Mobile >  Regex pattern to detect semicolon in a predefined string format
Regex pattern to detect semicolon in a predefined string format

Time:09-24

I have the following string:

A001-123;A002-12;B001-1

The pattern has to be as following:

  1. Always start with an alphabet followed with 3 numbers and dash -
  2. After the dash - it can be filled with random length of numbers
  3. It needs to separate between string with a semicolon ;

The set of string also can be more than 3, something like this should still be valid:

A001-123;A002-12;B001-1;B003-1
A001-123;A002-12

So these are some examples showing it's not a match:

A01-123;A002-12;B001-1
A001-123;A02-12;B001-1
A001-123,A002-12;B001-1
A001-123;A002-12;B001-1;

I need to know what is the regex pattern to achieve this.

Many thanks

CodePudding user response:

You can use this patttern: ^(?:[A-Z]\d{3}-\d*;)*(?:[A-Z]\d{3}-\d*$)

See Regex Demo

Note: this works if the string starts with an uppercase character (as in example)

  • Related