Regular express question

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have the following words, and I want to extract the first part and the
trailing numbers.

OP001 => ABC, 001
ST02 => CD, 02
00A => 00A, null

I have the following regular express:
(?<first>.*?)(?<second>\d+)
This one works well for the given first two examples but not the third one.

And (?<first>.*?)(?<second>\d+)$ returns strange result.
 
I'm unfamiliar with the <first> <second> syntax you're using, but the
following regular expression works for the three samples given:

Regex.Replace("OP001", @"(.*\D+)(\d*)", @"$1, $2"); // Returns: "OP, 001"
Regex.Replace("ST02", @"(.*\D+)(\d*)", @"$1, $2"); // Returns: "CD, 02"
Regex.Replace("00A", @"(.*\D+)(\d*)", @"$1, $2"); // Returns: "00A, "
 
I am using match.Groups.

Brandon Gano said:
I'm unfamiliar with the <first> <second> syntax you're using, but the
following regular expression works for the three samples given:

Regex.Replace("OP001", @"(.*\D+)(\d*)", @"$1, $2"); // Returns: "OP, 001"
Regex.Replace("ST02", @"(.*\D+)(\d*)", @"$1, $2"); // Returns: "CD, 02"
Regex.Replace("00A", @"(.*\D+)(\d*)", @"$1, $2"); // Returns: "00A, "
 
Yes, it works. Thanks.

However, there is another match, for a number

123 => 123, null

how to add this one to the regular express?
 
Sorry, should be

123 => null, 123

Matt said:
Yes, it works. Thanks.

However, there is another match, for a number

123 => 123, null

how to add this one to the regular express?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Back
Top