Reputation: 5
How can I remove leading zeros if the input is a number and otherwise not?
Specific scenario: I am only interested in the last 10 characters of the input. If this substring is consisting only of numbers, I want to remove the leading zeros. Otherwise, so if there is a word character or a special character, I need this whole substring.
Example 1
input: aaaa000002d111
expected: 000002d111 (because of the 'd' in the substring)
Example 2
input: aaaa0000011111
expected: 11111
I managed to remove the leading zeros with 0*(.{0,10})$
but how do I proceed in case any non-digit is included?
If possible, it would be perfect, if this expected substring is in group 1 of the match.
Upvotes: 0
Views: 4540
Reputation: 2852
.{4}(?:0*([1-9]{1,10}|\w{10}))$
skip the first 4 chars then search for either 10 digits (excluding any leading zeros) or the last 10 characters if not all digits. Both return in Group 1
see demo here
UPDATE
as per comments, changed the regex to allow 0
within the digits (after the first non-zero digit)
.{4}(?:0*([1-9]\d{1,9}|\w{10}))$
Upvotes: 1
Reputation:
This is one way
Find (?=\d{10}$)0*(\d+)$
Replace $1
Says remove leading zeros of last 10 digits.
Note that if the last 10 digits are all 0's, it will leave 1 behind.
Also, the assertion fixes the match position to the last 10 characters
so no need to worry about ranges after that.
Formatted
(?= \d{10} $ ) # Assert last 10 are digits
0* # Leading 0's
( \d+ ) # (1), The rest
$ # EOS
Upvotes: 0
Reputation: 7880
I think this will work:
(?=.{10}$)0*?([1-9][0-9]{0,9}|(?![0-9]*$).*)$
or
(?=.{10}$)0*?([1-9][0-9]{0,9}|.*[^0-9].*)$
First check that we are parsing the last 10 characters ((?=.{10}$)
).
Then lazily search for consecutive 0s, followed by a non-0 digit and other (at most 9) digits, or keep everything if there is at least a non-digit character.
In both the cases the output is stored in group 1, as required. See demo.
Upvotes: 0
Reputation: 19099
In Perl you can try this.
cat a |perl -npe 's/^[a-z]+[0]+([1-9][0-9]+)$|^[a-z]+([0]+[\d]+\w\d+)$/\1\2/'
Upvotes: 0