I'm struggling to get a regex expression to work.
I need to allow the following transforms based on the existence of leading zeros...
- 001234 -> 1234
- 1234 -> 1234
- 00AbcD -> AbcD
- 001234.1234 -> 1234.1234
- 001234.000002 -> 1234.2
- 001234/000002 -> 1234.2
I've found the expression matches works well for transforms 1, 2 & 3 but I'm not sure how to match the (optional) second section demonstrated in 4, 5 & 6.
^0*([0-9A-Za-z]*$)
You can get the zeros with following regex :
/(?:^|[./])0+/g
and replace the second group with first group (\1
).
For example in python i can do following :
>>> s="""001234
... 1234
... 00AbcD
... 001234.1234
... 001234.000002
... 001234/000002"""
>>> [re.sub(r'(:?^|[./])0+',r'\1',i) for i in s.split()]
['1234', '1234', 'AbcD', '1234.1234', '1234.2', '1234/2']