Got more questions? Find advice on: ASP | SQL | XML | Windows
in Search
Welcome to RegexAdvice Sign in | Join | Help

Need help with 5 part complex letter regex

Last post 04-15-2008, 11:18 AM by Lyndar. 3 replies.
Sort Posts: Previous Next
  •  04-14-2008, 8:26 AM 41337

    Need help with 5 part complex letter regex

    Hello,

    I have a text that goes like this:

     
    [LETTER]<FIRST_NAME>[@LAST_NAME@]_[ADDRESS]_<SEX>

     
    [] means that this part in brackets is NOT obligatory

    <> - means that his part in brackets is obligatory


    And possibilities go like this:

    LETTER- can be L (like Local), C (like Custom) or F. When LETTER is F (like Foreign) than part [ADDRESS] is obligatory. LETTER is not obligatory
    FIRST_NAME - can be LOCAL or MULTI (like multilingual). FIRST_NAME is obligatory
    LAST_NAME - any set of letters and '_' sign (underscore). Not obligatory
    Next we have underscore '_'
    After that there is ADDRESS is obligatory when LETTER = F. It consists of any set of letters only.
    Next we have another underscore '_'
    SEX is obligatory and it contains of any letter or underscore (for example 'm_f')
     

    Examples:

     [LETTER]<FIRST_NAME>[@LAST_NAME@]_[ADDRESS]_<SEX>

    LMULTI_MALE  means: local person with multilingual last name (without specifing), sex: male
    LLOCAL@SPEARS@_HOLLYWOOD_M_F  means: local person with local last name Spears, living in Hollywood, sex: male-female
     

     Could you please help me with this one?
    If it helps I'm writing a application in C# and need to parse this kind of string to get necessary information. And you know - 5 parts - its not for if else hardcoding

     

    greetings
     

    Filed under: , , , ,
  •  04-14-2008, 1:45 PM 41349 in reply to 41337

    Re: Need help with 5 part complex letter regex

    Here is something to try, tested in Expresso (minimally)

    (?<LETTER>[F]) (?<FIRST_NAME>LOCAL|MULTI)(?:@(?<LAST_NAME>[A-Z_]+?)@)?_(?<ADDRESS>[A-Z]+)_(?<SEX>[a-z_]+)
    |
    (?<LETTER>[LC])(?<FIRST_NAME>LOCAL|MULTI)(?:@(?<LAST_NAME>[A-Z_]+?)@)?_(?<ADDRESS>[A-Z]+)?_(?<SEX>[a-z_]+)

    If this does not quite fit your needs, you will need to provide us a more complete set of data, covering all options (i.e. if something is optional, include one case with it, one case without it, for all combinations).

    The regex is not optimized, its written for clarity.

  •  04-15-2008, 6:03 AM 41373 in reply to 41349

    Re: Need help with 5 part complex letter regex

    Hi,

     I've downloaded Expresso and tried this Regex you've written, it works, I'm just amazed, you are great, thank you.

    Just have a question. What if I would like for example in this place:

    (?<ADDRESS>[A-Z]+)?
     

    return custom text in place of match from the input. So I would like for example if this is matching:

     
    (?<LETTER>[LC])(?<FIRST_NAME>LOCAL|MULTI)(?:@(?<LAST_NAME>[A-Z_]+?)@)?_(?<ADDRESS>[A-Z]+)?_(?<SEX>[a-z_]+)

     

    return as ADDRESS not match from input text, but my own very custom text say for example: Unknown

     
    I've tried something like this (silly one):

    (?<LETTER>[LC])(?<FIRST_NAME>LOCAL|MULTI)(?:@(?<LAST_NAME>[A-Z_]+?)@)?_(?<ADDRESS>Unknown)?_(?<SEX>[a-z_]+)

     

    Obviously it doesn't work because it tries to match 'Unknown' string, so what to do to return my own text in this match?

     

    Kind regards, thanks again!
     

  •  04-15-2008, 11:18 AM 41387 in reply to 41373

    Re: Need help with 5 part complex letter regex

    I don't think any regex engine is designed to do that.  They are written to match text patterns and return what they matched (within capturing groups).

    However, if the capturing group is empty, it should be Nothing or empty string, so you can interpret that as Unknown within your code.

View as RSS news feed in XML