Namaskar
Namaskar

Reputation: 2119

Specific Length Regular Expression With Padding

Goal: to make a generalized Regular Expression for a fixed length string inside a larger string. This string has a specified padding character, followed by an integer counter that increments. Ideally, there would be some way to say, "I want this group to be of length 10 and contain only one type of character followed by a different character."

I am trying to match this within variable data (could be numbers could be letters could be symbols): The padding-characters + numbers add up to a specified length, here would be 5.

These are the allowed padding + number combinations.

$$$$1
$$$12
$$123
$1234

Here is an example:

<variable-data> <padding-characters> <numbers> <variable-data>
............... .................... ddddddddd ............... 
(where periods are any characters and 'd' is any digit)

Example Data:
ABC              $$$$                 1         $!@

Example Regex: 
ABC\$*\d+\$!@

Match: 
ABC$$$$1$!@
ABC$$$12$!@
ABC$$123$!@
ABC$1234$!@
ABC12345$!@

No Match:
ABC$$123456789$!@
ABC1$2$34$!@

Regex101

What I've Tried:

ABC(?=.{5})\$*\d+\$!@

This does not work because it still matches into the next digits because of \d+. Another thing I tried was

ABC(?=[\$\d]{5}[^\$\d])(\$*\d+)\$!@

Which aims to stop looking after it encounters a non-digit or non $, but that's not helpful since the next part of the string COULD start with a $ or a digit.

The easiest Regex to solve this:

(\$\$\$\$\d|\$\$\$\d\d|\$\$\d\d\d|\$\d\d\d\d|\d\d\d\d\d)

But I am trying to make this more generalized, and there can be a variable amount of padding E.G.

$$$$$$$$$1
$$$$$$$$12
...

Upvotes: 1

Views: 2312

Answers (2)

trincot
trincot

Reputation: 350270

You could look ahead to check that you don't have an inverted sequence of padding character and digit within the scope of the next 5 characters, and then require and capture 5 characters that are only digits and padding characters:

      ABC(?!.{0,3}\d\$)([\$\d]{5})\$!@

If you need at least one digit, then:

      ABC(?!.{0,3}\d\$)([\$\d]{4}\d)\$!@

Upvotes: 3

squirl
squirl

Reputation: 1784

ABC(?=.{5}\$!@)\$*\d+\$!@

This is very similar to your first attempt, but with the slight difference that the lookahead also contains the terminating string. This gives it something to anchor to, to make sure the regex doesn't match anything more.

Upvotes: 2

Related Questions