regex to extract number from a chain type "height/width/lenght: 12.5/15.4/14"

From the expression "height/width/lenght: 12.5/15.4/14" I would like to extract:

height 12.5
width  15.4
lenght 14

I tried but it is too much for a regex beginner.
thanks for your support
novreisbAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

käµfm³d 👽Commented:
What programming language or text editor are you using? In its most simplistic form, this may suffice:

(\d+(\.\d+)?)/(\d+(\.\d+)?)/(\d+(\.\d+)?)

Open in new window


Inspect capture groups 1, 3, & 5.

Depending on what regex engine you have available to you, you can get more elaborate:

(\d+(?:\.\d+)?)/(\d+(?:\.\d+)?)/(\d+(?:\.\d+)?)

Open in new window


Inspect capture groups 1, 2, & 3.

(?<height>\d+(?:\.\d+)?)/(?<width>\d+(?:\.\d+)?)/(?<length>\d+(?:\.\d+)?)

Open in new window


Inspect capture groups "height", "width" and "length".
0
novreisbAuthor Commented:
I'm glad you answered as far I can see, you are the "best".

I'm using c# and inspecting a RichTextBox on a Windows Forms app.
I already understand that I was not clear enough. Sorry! I will try explain better.

height, width and lenght were just examples.

Imagine
                 xxx/aaa/bbb: 12.4/25/23.5
                 aaa/bbb/xxx: 25/23.5/12.4
                 aaa/bbb/xxx/yyy: 25/23.5/12.4/30

I'm looping for each string so, if i'm working with xxx I need to have always 12.5 . What I want is the number in same relative position. Hope it is clear now!
thks kaufmed foy your availability
0
käµfm³d 👽Commented:
If you can have an arbitrary number of items, then I don't think regex is a good choice. A couple of Splits should suffice.

e.g.

static string GetValueByKey(string key, string keyValues)
{
    string[] keysAndValues = keyValues.Split(':');
    string keys = keysAndValues[0];
    string values = keysAndValues[1];
    int targetIndex = -1;

    foreach (string availableKey in keys.Split('/'))
    {
        targetIndex++;

        if (string.Equals(key, availableKey.Trim(), StringComparison.OrdinalIgnoreCase))
        {
            break;
        }
    }

    if (targetIndex < 0)
    {
        throw new ApplicationException("Key not found!");
    }

    string[] availableKeys = values.Split('/');

    if (targetIndex >= availableKeys.Length)
    {
        throw new ApplicationException("Not enough values supplied!");
    }

    return availableKeys[targetIndex].Trim();
}

Open in new window


Usage
string keyValues = "xxx/aaa/bbb: 12.4/25/23.5";
//string keyValues = "aaa/bbb/xxx: 25/23.5/12.4";
//string keyValues = "aaa/bbb/xxx/yyy: 25/23.5/12.4/30";
string value = GetValueByKey("xxx", keyValues);

Open in new window


There's minimal error handling in the above function. You'll need to add in whatever you think you might encounter in production.
0
novreisbAuthor Commented:
that's perfect for my example!
I was looking for regex once there are guys that are using " " instead  the ":" And this is just an example!
I talk about emails from hundreds/thousands guys about a specific theme, so the main keywords are known! Terrible to get the good numbers, sometimes! As an example, if I have someone writing "width" instead "width", or "tmw" instead "twm", how can I guess?
Thks very much for your help, but I see (magical) regex cannot help me always!
0
käµfm³d 👽Commented:
If you had a fixed number of items, then you could use a regex. For example:

static string GetValueByKey(string key, string keyValues)
{
    System.Text.RegularExpressions.Match match = System.Text.RegularExpressions.Regex.Match(keyValues, "([^/]+)/([^/]+)/([^/]+):([^/]+)/([^/]+)/([^/]+)");

    for (int i = 1; i <= match.Groups.Count / 2; i++)    // Divide by two to inspect the left side of the colon
    {
        if (string.Equals(match.Groups[i].Value.Trim(), key, StringComparison.OrdinalIgnoreCase))
        {
            return match.Groups[i + 3].Value.Trim();    // Add 3 to get the matching capture group for the items on the right side of the colon
        }
    }

    return null;
}

Open in new window


In this example, the pattern is fixed to locate 3 items (on each side of the colon). This means that your third example could not be matched because it has 4 items. If you knew for sure that you'd only ever have 3 items, then you could use the above. Of course, this isn't limited to just 3 items. You just have to make sure that every possible input string you could have would only have the number of items that you specify. If it's 3, then you can only send in strings that have 3 items; if it's 4, then you can only send in strings that have 4 items; etc.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Development

From novice to tech pro — start learning today.