We’d like to remind Forumites to please avoid political debate on the Forum.

This is to keep it a safe and useful space for MoneySaving discussions. Threads that are – or become – political in nature may be removed in line with the Forum’s rules. Thank you for your understanding.

📨 Have you signed up to the Forum's new Email Digest yet? Get a selection of trending threads sent straight to your inbox daily, weekly or monthly!
The Forum now has a brand new text editor, adding a bunch of handy features to use when creating posts. Read more in our how-to guide

Shortest match regex - any help

Horlock
Horlock Posts: 1,027 Forumite
Hi hope this a suitable place to put this.

I'm trying to extract an expression in visual basic .net (but any regex help greatly appriciated).

my starting string is something like:

hello <personname> john <or/> fred <or/> terry <or/> bill <or/> jane </personname>

I want grab the person names!

As a start I would be happy with how to grab john.

My guess was

RegularExpressions.Regex.Replace(input, ".*<personname>(.*)<or/>.*", "$1").ToString

however this grabs

john <or/> fred <or/> terry <or/> bill <or/> jane

Unfortunately I can't just do

RegularExpressions.Regex.Replace(input, ".*>(^<*)<.*", "$1").ToString

because there is other markup in the string which is needed (ie the original string is actually slightly more like

hello <personname> john <or/> <dontlike> fred </dontlike> <or/> terry <or/> bill <or/> jane </personname>

Any help greatfully recieved (assuming it's possible - and I've posted in the correct place).

Ta
There is no intelligent life out there ... ask any goldfish!

Comments

  • irnbru_2
    irnbru_2 Posts: 1,603 Forumite
    Horlock wrote:
    hello <personname> john <or/> fred <or/> terry <or/> bill <or/> jane </personname>

    Is that actually what you're given? It's not part of some properly tag-balanced document?
  • Horlock
    Horlock Posts: 1,027 Forumite
    irnbru wrote:
    Is that actually what you're given? It's not part of some properly tag-balanced document?
    It is part of a tag-balance document - if that helps
    There is no intelligent life out there ... ask any goldfish!
  • Horlock wrote:
    It is part of a tag-balance document - if that helps
    In that case it might be easier to parse it as an XML document - see http://www.codeproject.com/vb/net/parsefilecode.asp.
  • Horlock
    Horlock Posts: 1,027 Forumite
    In that case it might be easier to parse it as an XML document - see http://www.codeproject.com/vb/net/parsefilecode.asp.
    Wow that would probably be marvelous if I hadn't gone so far in the code already - still it might be worth my while to backtrack and use it - but there are quite a few classes which might need to be scrapped - though it may end more elegantly - I'll have to think about it.

    Thanks loads though - afraid I can only click thanks once!
    There is no intelligent life out there ... ask any goldfish!
  • Horlock
    Horlock Posts: 1,027 Forumite
    Just found an easy work around - I think - but I'm not going to implement it tonight.

    If I start from the other end.

    Ie first I find Jane
    then I find bill etc etc

    I'm fairly confident that that will work
    There is no intelligent life out there ... ask any goldfish!
This discussion has been closed.
Meet your Ambassadors

🚀 Getting Started

Hi new member!

Our Getting Started Guide will help you get the most out of the Forum

Categories

  • All Categories
  • 353.6K Banking & Borrowing
  • 254.2K Reduce Debt & Boost Income
  • 455.1K Spending & Discounts
  • 246.7K Work, Benefits & Business
  • 603.1K Mortgages, Homes & Bills
  • 178.1K Life & Family
  • 260.7K Travel & Transport
  • 1.5M Hobbies & Leisure
  • 16K Discuss & Feedback
  • 37.7K Read-Only Boards

Is this how you want to be seen?

We see you are using a default avatar. It takes only a few seconds to pick a picture.