?
Solved

regular expressions, find text that starts with a word ends with another word but does not contain the last word.

Posted on 2003-10-27
3
Medium Priority
?
1,543 Views
Last Modified: 2007-12-19
Quick one for anyone good with regular expressions.

Ok, say I have a XML file with this layout.

<event  time="23424" name="2325">
      <param name="5645645" data="1231">
      <param name="11231213" data="346">
      <param name="2351324" data="123">
</event>
<event  time="43747" name="2435">
      <param name="32452345" data="5674">
      <param name="2345" data="4567">
      <param name="2435" data="2345">
</event>

I want to use regular expressions to find blocks missing the </event> tag.

I can search and find the blocks from <event...> to the next <event...> but I can't figure out how to only list ones without </event>

Thanks
0
Comment
Question by:waynegs
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 2

Accepted Solution

by:
ultimatemike earned 1000 total points
ID: 9633516
It's kinda tricky to do with regular expressions....Generally XML/HTML is recursive, and it's structure doesn't lend well to RE's.  Here's a perl script that does the job though - It'll print out and event tags that aren't closed:


Just change "xmlfile.xml" to whatever the filename is.


#!perl -w
use strict;


      open XML, "xmlfile.xml";

      $_ = <XML>;


s/<event/#/gm;
s/>\s*</></gm;


my @array = split /#/;


foreach my $element (@array) {
      if  ( $element && $element !~ /<\/event>$/g ) {
            print "FOUND: ";
            print "<event$element\n";
            
      }
};
0
 
LVL 2

Expert Comment

by:scully00000
ID: 9636188
What language are you using? You could use a regex to find the blocks and then search recursively through them for ones missing the </event> tag. Also, what do you want to do with the 'blocks' when you've flagged them? Has a bearing on how the script is written.

Cheers
0
 
LVL 8

Expert Comment

by:fozylet
ID: 9636224
Try your luck at http://www.regexp.org/

A previously answered question there may fit your need.
0

Featured Post

7 Extremely Useful Linux Commands for Beginners

Just getting started with Linux? Here's a quick start guide that has 7 commands that we believe will come in handy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I will show you how to create a ASP.NET Captcha control without using any HTTP HANDELRS or what so ever. you can easily plug it into your web pages. For Example a = 2 + 3 (where 2 and 3 are 2 random numbers) Session("Answer") = 5 then we…
Styling your websites can become very complex. Here I'll show how SASS can help you better organize, maintain and reuse your CSS code.
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question