Link to home
Start Free TrialLog in
Avatar of novreisb
novreisb

asked on

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
Avatar of kaufmed
kaufmed
Flag of United States of America image

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".
Avatar of novreisb
novreisb

ASKER

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
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.
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!
ASKER CERTIFIED SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial