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

Catching optional data

Last post 07-26-2010, 6:09 PM by buckley. 1 replies.
Sort Posts: Previous Next
  •  07-26-2010, 9:57 AM 70112

    Catching optional data

    Hi - very new to regexes - so apologies

     I'm trying to catch data which can be optional after the first entry

    i.e. 

    Test cases

    <one><end>

    <one> | <two><end>

    <one> | <two> | <three><end>

    gives answer groups as follows

    one

    one two

    one two three

     

    and so on.

    There is a finite number of optionals but the number is unknown.

    I tried 

    <([^>]*)>( | <([^>]*)>)*?<end>

    but I think I'm getting caught out by using the incorrect identifier to get the contents between the <> AND also being too 'greedy':-(

    Could someone please give me advice on the best way to approach this ?

    Cheers

     

  •  07-26-2010, 6:09 PM 70141 in reply to 70112

    Re: Catching optional data

    Hi,

    Pattern :

     

    (I have to use a screenshot since this forum messes up tag like sequences)

    Replace With :

    $1

    Using C# (state your language if it can help you)

    resultString = Regex.Replace(subjectString, @"<(.*?)>.*?(\||<end>)", "$1", RegexOptions.Singleline);

     Given this as input :

     <one><end>

    <one> | <two><end>

    <one> | <two> | <three><end>

     It will produce this output :

    one

    one two

    one two three 

     

    HTH, Tom 

View as RSS news feed in XML