user3583807
user3583807

Reputation: 800

regexp single digit at start of string

A have following alphanumeric sequence:

[9A1 9A10 9B2 10C1]

I want to transform it in following way:

9A1  --> 096501
9A10 --> 096510
9B1  --> 096601
10C1 --> 106701

I need it to perform sorting. So can't get regexp that matches to only 1 digit at the start of string, e.g. it should return me following:

9A1  --> 9
9A10 --> 9
9B1  --> 9
10C1 -->  {nothing}

So I can transform it then to two digit presentation.

I tried following:

^[\d]{1}

but it works for all cases not for only single digit.

Upvotes: 0

Views: 42

Answers (1)

The fourth bird
The fourth bird

Reputation: 163632

You could match a digit from the start of the string ^\d and use a negative lookahead (?!\d) to assert what is directly on the right is not a digit:

^\d(?!\d)

Regex demo

Note that ^[\d]{1} can be written as ^\d and matches a single digit from the start of the string. It does not take into account what is directly on the right.

Upvotes: 3

Related Questions