1

I have a specific pattern I want to follow but the extraction process is not working as expected. I assume the pattern I developed is not correct, but I can't find the issue in it.

I have the string string test1 = "R1 0.1uF" and the pattern

"(?<des>^[a-zA-Z]+)|(?<number>\d+\s+)|(?<val>[0-9]*.?[0-9]+)|(?<units>[^,]*)";

I want it to be extracted as follows:

des: R

number: 1

val: 0.1

units: uF

Currently, des is working properly and it's finding R but the others return an empty string.

Here's my code

const string pattern = @"(?<des>^[a-zA-Z]+)|(?<number>\d+\s+)|(?<val>[0-9]*.?[0-9]+)|(?<units>[^,]*)";

string test1 = "R1 0.1uF";

Regex r = new Regex(pattern, RegexOptions.Compiled);

Match m = r.Match(test1);
string des = m.Groups["des"].Value;
string number = m.Groups["number"].Value;
string val = m.Groups["val"].Value;
string units = m.Groups["units"].Value;

4
  • 2
    Why are you joining the parts with |? That means "OR" -- it's matching the des bit, and then because that matched, it's not trying any of the other bits Commented Feb 19, 2020 at 15:07
  • My bad, I completely overlooked that. Thanks for the help! Commented Feb 19, 2020 at 15:10
  • 1
    You could remove the | and make it a bit more specific ^(?<des>[a-zA-Z]+)(?<number>[0-9]+)\s+(?<val>[0-9](?:\.[0-9]+)?)(?<units>[^,]*) Commented Feb 19, 2020 at 15:10
  • 1
    Try following : const string pattern = @"(?<des>[a-zA-Z]+)(?<number>[^\s]+)\s(?<val>[0-9]*.?[0-9]+)(?<units>.*)"; Commented Feb 19, 2020 at 15:23

1 Answer 1

3

Two comments:

  1. Remove all the |s because you don't want to match des OR number OR val OR units but des followed by number followed by val followed by units
  2. Escape the dot: \. because you want to match a dot, not some random character.

So, your regex becomes

(?<des>^[a-zA-Z]+)(?<number>\d+\s+)(?<val>[0-9]*\.?[0-9]+)(?<units>[^,]*)

Look at regex101

Some little improvements, if I may:

  • Move the caret ^ to the front. It doesn't belong in the first group.
  • Do not capture the space(s) in the group number.

Like this:

^(?<des>[a-zA-Z]+)(?:(?<number>\d+)\s+)(?<val>[0-9]*\.?[0-9]+)(?<units>[^,]*)

See regex101

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

Comments

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.