0

I need a regular expression to match a string within a longer string. Specifically I need to not match any leading zeros or the last 2 digits for the string.

For example, my input might be the following: 00009666666605 00010444444404 00007Z22222205 00033213433104 00009000G00005

And I would like to match 96666666 104444444 7Z222222 332134331 9000G000

For further information, the last 2 digits are always numbers and describe the starting point of the valid reference, after the leading zeros.

I thought I'd cracked it with something like (?<=0000).{8}|((?<=000).{9})+? but that doesn't work as expected.

2
  • 1
    I need a regular expression why must it be a RegEx? Commented Feb 12, 2020 at 18:48
  • @djv I'm using an existing regex .net vbo throughout my process so much easier to add in another regex, rather than create an entirely new vbo. Commented Feb 12, 2020 at 19:30

2 Answers 2

2

It sure takes a lot of steps, but this should do the trick:

(?<=^000)[^0].{8}|(?<=^0000).{8}

(?<=     'start lookbehind
   ^000  'for the beginning of the string then three zeroes
)        'end lookbehind
[^0]     'match a non-zero
.{8}     'match the remaining 8 chars
|        ' OR
(?<=     'start lookbehind
   ^0000 'for the beginning of the string then four zeroes
)        'end lookbehind
.{8}     'match the remaining 8 chars

That said, in .NET, it will be quicker to do:

dim trimmed = line.TrimStart("0"c)
dim numberString = trimmed.Substring(0,trimmed.Length-2)

if the format of these string is always the same

Sign up to request clarification or add additional context in comments.

1 Comment

@ScottMeadmore Please accept this answer by clicking the check mark to the left of the answer. This will thank the poster and earn you a few points.
2

I would use:

^0*(.*).{2}$

And access your matches via $1

Regex Storm demo

3 Comments

This one didn't work in .net using regex.match strangely
@ScottMeadmore Why did it fail for you?
This works and is much more efficient than the Regex you picked. You just have to handle the line feeds. It would be better to read each line in a loop (maybe yield return the match). Some thing like: var sb = new StringBuilder(); foreach (string line in [Input[]]) { sb.AppendLine(Regex.Match(line, "^0+(.*).{2}$").Groups[1].Value); }. If you want to treat text with \r\n as new line, then you could add a line feed at the end. Something like:: string txt = [InputText] + "\r\n"; var matches = Regex.Matches(txt, "0+(.+).{3}");

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.