Horse Voice
Horse Voice

Reputation: 8338

regex to strip leading zeros treated as string

I have numbers like this that need leading zero's removed.

Here is what I need:

00000004334300343 -> 4334300343

0003030435243 -> 3030435243

I can't figure this out as I'm new to regular expressions. This does not work:

(^0)

Upvotes: 30

Views: 73855

Answers (10)

Dan M
Dan M

Reputation: 19

In Azure Synapse, the expression would be this:

regexReplace(ColumnName, "^0+", "")

000000502842 will equal 502842

Upvotes: 1

patrick udochukwu
patrick udochukwu

Reputation: 11

String str = "0600974";
str = str.replaceAll("\\b0+(\\d+)\\b", "$1");

This is a better match, Code is in java, to use in other languages you can replace double slash with one e.g "\\b" will be "\b" and "\\d" will be "\d"

Upvotes: 1

Pravin Chukkala
Pravin Chukkala

Reputation: 9

Here is the simple and proper solution.

str = str.replaceAll(/^0+/g, "");

Global Flag g is required when using replaceAll with regex.

Upvotes: 0

Kruglord
Kruglord

Reputation: 31

I know this is an old question, but I think the best way to do this is actually

str = str.replaceAll("(^0+)?(\d+)", "$2")

The reason I suggest this is because it splits the string into two groups. The second group is at least one digit. The first group matches 1 or more zeros at the start of the line. However, the first group is optional, meaning that if there are no leading zeros, you just get all of the digits. And, if str is only a zero, you get exactly one zero (because the second group must match at least one digit).

So if it's any number of 0s, you get back exactly one zero. If it starts with any number of 0s followed by any other digit, you get no leading zeros. If it starts with any other digit, you get back exactly what you had in the first place.

Upvotes: 3

Artur  Dumchev
Artur Dumchev

Reputation: 1330

Accepted solution will fail if you need to get "0" from "00". This is the right one:

str = str.replaceAll("^0+(?!$)", "");

^0+(?!$) means match one or more zeros if it is not followed by end of string.

Thank you to the commenter - I have updated the formula to match the description from the author.

Upvotes: 18

Luis Colorado
Luis Colorado

Reputation: 12668

\b0+\B will do the work. See demo \b anchors your match to a word boundary, it matches a sequence of one or more zeros 0+, and finishes not in a word boundary (to not eliminate the last 0 in case you have only 00...000)

Upvotes: 3

Rohit Jain
Rohit Jain

Reputation: 213223

You're almost there. You just need quantifier:

str = str.replaceAll("^0+", "");

It replaces 1 or more occurrences of 0 (that is what + quantifier is for. Similarly, we have * quantifier, which means 0 or more), at the beginning of the string (that's given by caret - ^), with empty string.

Upvotes: 50

MaxZoom
MaxZoom

Reputation: 7753

The correct regex to strip leading zeros is

str = str.replaceAll("^0+", "");

This regex will match 0 character in quantity of one and more at the string beginning. There is not reason to worry about replaceAll method, as regex has ^ (begin input) special character that assure the replacement will be invoked only once.

Ultimately you can use Java build-in feature to do the same:

String str = "00000004334300343";
long number = Long.parseLong(str);
// outputs 4334300343

The leading zeros will be stripped for you automatically.

Upvotes: 1

SubOptimal
SubOptimal

Reputation: 22973

Another solution (might be more intuitive to read)

str = str.replaceFirst("^0+", "");

^ - match the beginning of a line
0+ - match the zero digit character one or more times

A exhausting list of pattern you can find here Pattern.

Upvotes: 4

anubhava
anubhava

Reputation: 784998

If you know input strings are all containing digits then you can do:

String s = "00000004334300343";
System.out.println(Long.valueOf(s));
// 4334300343

Code Demo

By converting to Long it will automatically strip off all leading zeroes.

Upvotes: 5

Related Questions