1

I want to know how to match a string after zeros at the beginning of a string.

so example cases of what the string should match are

ABC0123 -> ABC0123
0ABC0123 -> ABC0123
000ABC0123 -> ABC0123

I have tried [^0]+$ but that would return the below which is wrong

   ABC0123 -> 123
    0ABC0123 -> 123
    000ABC0123 -> 123
Darrell123
  • 53
  • 6

4 Answers4

1

Using [^0]+$ will match any char other than 0 till the end of the string. That match will start after the last zero in 0123 and will give you 123 as a match.

To get a match only, you can start the match with any character except a 0 or newline and then match optional characters.

[^0\n\r].*

Regex demo

Or you can match optional leading zeroes, and capture the rest of the string in group 1.

If you only want to match non whitespace chars, you can use \S+. To match any char except newline characters, you can use .+

^0*(\S+)

Regex demo

The fourth bird
  • 154,723
  • 16
  • 55
  • 70
  • Thanks for this, But the Match group matched the whole string I was hoping for something that matched only everything after the leading zeros – Darrell123 Aug 21 '21 at 10:40
  • @Darrell123 The value after the zeroes is in capture group 1 (Highlighted in green in the regex101 link) https://regex101.com/r/4Y4PC3/1 What is the tool or language? – The fourth bird Aug 21 '21 at 10:42
  • The tool has function SUBSTR(string.RegEx) which returns the portion of the string matched by the regular expression. So if it matched the whole string it will just give me that same thing again – Darrell123 Aug 21 '21 at 10:49
  • You could try `^0*\K\S+` https://regex101.com/r/9aEPQl/1 or `(?<=^0*(?!0))\S+` https://regex101.com/r/fcFH7C/1. Perhaps you can also use group 1 in the replacement, oftern denoted by `\1` or `$` or see if there is a match function and then get the value of group 1. – The fourth bird Aug 21 '21 at 11:17
  • @Darrell123 I have updated the answer to get a match only. – The fourth bird Aug 21 '21 at 21:06
0

Start with a word char except a 0, then word chars:

[\w&&[^0]]\w+

See live demo.

Bohemian
  • 412,405
  • 93
  • 575
  • 722
0

Matching a string after zeros at the beginning of a string is possible with a positive lookbehind like

(?<=^0*)[^0].*

The (?<=^0*) lookbehind matches a location that is immediately preceeded with zero or more 0 chars at the start of string, and [^0].* will match a char other than 0 and the rest of the string. See this regex demo.

In some regex flavors (like PCRE or Onigmo), you can use \K match reset operator:

^0*\K.+

The ^0* will match zero or more 0 chars at the start of the string and \K will discard these matched chars from the match memory buffer. See this regex demo.

In the ICU regex flavor (R stringr functions, Swift) or Java, you can use constrained-width lookbehind pattern. Say, you know there can be no more than 100 zeros on front. Then you can use

(?<=^0{0,100})[^0].*

The (?<=^0{0,100}) lookbehind matches a location that is immediately preceeded with zero to 100 zeros at the start of string, and [^0].* will match a char other than 0 and the rest of the string. See this regex demo.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
0

The simplest way is to start the match at the first non-zero.
This is done with a simple lookbehind after the current sample character.
And this will not match an empty string.

.(?<!0).*

https://regex101.com/r/EeyYQU/1

sln
  • 2,071
  • 1
  • 3
  • 11