I am trying to remove the leading zeroes 0011223344 and also leading and trailing white spaces
/^0+(?=[0-9]/
s/^\s+|\s+$//
How can I combine the two to get the same output.
11223344
I am trying to remove the leading zeroes 0011223344 and also leading and trailing white spaces
/^0+(?=[0-9]/
s/^\s+|\s+$//
How can I combine the two to get the same output.
11223344
You may use this regex in perl:
s/^\h*0*(?=\d)|\h+$
RegEx Details:
\h matches a horizontal whitespace^\h*0*(?=\d): At the start it will match 0 or more leading whitespaces followed by 0 or more leading zeroes as long as there is at least one digit ahead| OR\h+$: At the end it will match 1+ horizontal whitespacesExamples:
perl -pe 's/^\h*0+(?=\d)|\h+$//g' <<< ' 001 '
1
perl -pe 's/^\h*0+(?=\d)|\h+$//g' <<< ' 000 '
0
perl -pe 's/^\h*0+(?=\d)|\h+$//g' <<< ' 0000000123 '
123
perl -pe 's/^\h*0*(?=\d)|\h+$//g' <<< ' 123 '
123
You may use
s/^[\s0]+|\s+$//g
Or, for a corner case like ' 0000 ' where you would still like to keep one zero:
s/^(?:\s*(0)+\s*$|[\s0]+)|\s+$/$1/g
See the regex demo #1 and regex demo #2.
Details
^[\s0]+ - matches one or more zeros or whitespace at the start of the string^(?:\s*(0)+\s*$|[\s0]+) - matches
^ - start of string(?:\s*(0)+\s*$|[\s0]+) - either of
\s*(0)+\s*$ - 0+ whitespaces, 1 or more zeros each time captured into Group 1, and then 0+ whitespaces till end of string|- or[\s0]+ - 1 or more whitespaces or zeros| - or\s+$ - matches one or more whitespace chars at the end of string.