Multiple submatches from one string

Results 1 to 2 of 2

Thread: Multiple submatches from one string

  1. #1
    Join Date
    Dec 1969

    Default Multiple submatches from one string

    Is it possible to use one one submatch in the regular expression to return more than one submatch in the submatches collection?<BR><BR>Consider: You&#039;re trying to match an image tag, and extract the attributes into submatches at the same time.<BR><BR>Your tag could be:<BR>&#060;img src="image.gif" alt="image"&#062;<BR>or<BR>&#060;img width=400 src="image.gif" alt="image" border=0&#062;<BR><BR>The Regular Expression I&#039;ve come up with is "&#060;s*img(?:s+(ws*=s*".*?"))*s*&#062;", where I&#039;m trying to match as many attributes as possible before the closing tag, storing just the &#039;attribute="value"&#039; as a submatch.<BR><BR>My submatches collection always contains only one item - the last submatch. I suspect that each new occurrence is overwriting the previous one. How do I tell it to store each submatch as a new item, rather than overwriting the old one?<BR><BR>It&#039;s my first Regular Expression, btw

  2. #2
    Join Date
    Dec 1969

    Default RE: Multiple submatches from one string

    Yes I can see that :O)<BR>What attributes are you interested in ??<BR><BR>if you&#039;re interested in src then this will help you:<BR><BR>sPattern = "&#060;img srcs?=(u0022&#124027s)?(.*?)(u0022&#124027&#124s )?"<BR>Then submatch(1) will contain image.gif !<BR><BR>you should have in mind that there&#039;s no guarantee for that the img attributes are in any particularly order, someone could write the alt= first and then src= !!<BR>To solve this problem you need to make a little loop, matching each interesting attribute one at a time! HINT: use my pattern and replace the SRC with a variable and do the loop on that!<BR><BR>If you&#039;re interested in all with a &#039;=&#039; then you should repeat my pattern without the &#039;src&#039;, but in my opinion it&#039;s a bad solution!<BR><BR>Janus

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts