Regular Expressions matching difficulty

My current regex:


Right so I am attempting to make regex match a string based on: a count that can be any amount of number from 0-1million then followed by a number then sometimes a letter then - then any number for numbers followed by the same number and sometimes a letter then sometimes a letter. example of strings it should match:


an example of what it should return based on above (this is 2 examples it shouldn’t read both lines.)

(192) (1-122010408174) (1) (b)
(19212321) (2a-122023410408174) (2a) (b)

My current regex works with the second one but it returns (1b) in the first when I would like it to return (1) (b) but also return (2a) in the case of the second one or the case of:

1926h-1220104081746h Should Return: (192) (6h-122010408174) (6h)

Not 100% sure if its possible, sense I’m fairly new to regex. For reference I’m doing this in excel-vba if there is any other way to do this easier.


You could capture the character(s) before the dash character, and then back reference that match.

In the expression below, \3 would match what was matched by the 3rd capturing group:


Example Here

enter image description here

Output after merging the capture groups:

(192) (1-122010408174) (1) (b)

(19212321) (2a-122023410408174) (2a) (b)

(192) (6h-122010408174) (6h)


Disregard the JS. Here is the output after merging the capture groups:

var strings = ['1921-1220104081741b', '192123212a-1220234104081742ab', '1926h-1220104081746h'], exp = /(\d*)((\d[a-z]*)-\d*)(\3)([a-z])?/;
strings.forEach(function(str) {
var m = str.match(exp);
  snippet.log('(' + m[1] + ') ('+ m[2] + ') (' + m[4] + ') (' + (m[5]||'') + ')');
<script src=""></script>