0

Best way to extract specific numbered word from text field

Hi Everyone - first question, please be gentle with me :) :) :) 

Initial content in field Customer_description is: "NAME: Richard James (ID 230) COUPON:"

I want to retrieve 'Richard' so I create a field called Calcfirstname
and write the following formula:

let myfirstcount := index(Customer_description, " ");
let mysecondtext := substr(Customer_description, myfirstcount + 1, 90);
let mysecondcount := index(mysecondtext, " ");
substr(mysecondtext, 0, mysecondcount)

Is there not a better quicker way to do that?????

I haven't written any sort of code for a couple of years and fear that I'm as rusty as an old nail :)

Thanks.

6 replies

null
    • Sean
    • 4 yrs ago
    • Reported - view

    The easiest solution to implement would be something like this...

     

    extractx(Customer_description, "NAME: (\w+)", "$1")

     

    If you want to understand it, here is a resource I like...

     

    https://www.rexegg.com/

    https://www.rexegg.com/regex-quickstart.html

    • Winghigh Limited
    • Robert_AgarHutton
    • 4 yrs ago
    • Reported - view

    Hi Sean,

    Thanks.

    I read some of the info on the linked site and now my brain hurts - I suppose I was hoping for some kind of command like: word(Customer_description, 2) - Oh well that would have been way too easy :)

    I'll have to read more of the rexegg site or just make do with ugly code - at least the overhead of a bit of ugly code won't be too bad (I hope).

    Have a great weekend.

    • Sean
    • 4 yrs ago
    • Reported - view

    Robert,

    You're welcome. I felt the same way when I started learning regex. You could always create a user-defined function that takes those 2 arguments 😬. Just in case you haven't found the language reference...

     

    https://ninox.com/en/manual/calculations/reference-of-functions-and-language

     

    You have a great weekend also.

    • Sean
    • 4 yrs ago
    • Reported - view

    This gets me to post 1000!

     

    function word(arg : number) do
    extractx(Customer_description, "NAME: (\w+) (\w+) (\(ID \d+\))", text("$" + arg))
    end;
    word(ArgNum)

     

    That user-defined funtion will return first name with ArgNum = 1, last name with ArgNum = 2 and ID with ArgNum = 3. You just have to declare the function before you use it.

    • Winghigh Limited
    • Robert_AgarHutton
    • 4 yrs ago
    • Reported - view

    Hi Sean - Congrats on your 1000th :) and thanks for the code.

    • Sean
    • 4 yrs ago
    • Reported - view

    Hi Robert, I remembered that the Ninox compiler will automatically cast a number as text if one of the terms is text and the "+" operator is used. So...

     

    extractx(Customer_description, "NAME: (\w+) (\w+) (\(ID \d+\))", text("$" + arg))

     

    can be replaced with...

     

    extractx(Customer_description, "NAME: (\w+) (\w+) (\(ID \d+\))", "$" + arg)