Scott Ruiter
Scott Ruiter

Reputation: 13

Match value after semicolon without including comma

I need to match the string after the semicolon while not including the comma, i.e. I am specifically looking for the values 1, 2, 3, 4, 5, 6, etc.

Here is the input:

C07KTL89290003;1,C07KTL89290003,1;2,C07KTL89290003,0;3,C07KTL89290003,0;4,C07KTL89290003,0;5,C07KTL89290003,0;6,C07KTL89290003,0;7,C07KTL89290003,0;8,C07KTL89290003,0;9,C07KTL89290003,0;10,C07KTL89290003,0;11,C07KTL89290003,0;12,C07KTL89290003,0;13,C07KTL89290003,0;14,C07KTL89290003,0;15,C07KTL89290003,0;16,C07KTL89290003,0;17,C07KTL89290003,0;18,C07KTL89290003,0;19,C07KTL89290003,0;20,C07KTL89290003,0;21,C07KTL89290003,0;22,C07KTL89290003,0;23,C07KTL89290003,0;24,C07KTL89290003,0;25,C07KTL89290003,0;26,C07KTL89290003,0;27,C07KTL89290003,0;28,C07KTL89290003,0;29,C07KTL89290003,0;30,C07KTL89290003,0;31,C07KTL89290003,0;32,C07KTL89290003,0;33,C07KTL89290003,0;34,C07KTL89290003,0;35,C07KTL89290003,0;36,C07KTL89290003,0;37,C07KTL89290003,0;38,C07KTL89290003,0;39,C07KTL89290003,0;40,C07KTL89290003,0

The expression I am using right now is

\d+[,]+[^,]+[^,0-2]

However this is returning the number after a ;, a comma and the substring till the next comma.

Can someone please help me modify my expression to match only the digits after the semicolon while not including the comma?

Upvotes: 1

Views: 61

Answers (2)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 627103

In .NET, C# in your case, you may use

(?<=;)\d+

Or, if you must specify that a valid match is only when a comma follows the digits:

(?<=;)\d+(?=,)

See the .NET regex demo and the regex graph:

enter image description here

Details

  • (?<=;) - a positive lookbehind that requires a ; to appear immediately to the left of the current location
  • \d+ - 1+ digits
  • (?=,) - (not really necessary here) a positive lookahead that requires a , to appear immediately to the right of the current location.

C# snippet:

var results = Regex.Matches(text, @"(?<=;)\d+")
        .Cast<Match>()
        .Select(x => x.Value);

Upvotes: 1

user11116003
user11116003

Reputation:

Try this:

\d+[,]+\K[^,]+[^,0-2]

Here Is Demo

Upvotes: 0

Related Questions