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
1,522 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
3 Comments
 
LVL 2

Accepted Solution

by:
ultimatemike earned 250 total points
Comment Utility
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
Comment Utility
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
Comment Utility
Try your luck at http://www.regexp.org/

A previously answered question there may fit your need.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

It's sometimes a bit tricky to use date functions in Oracle BPEL. I'll explain quickly how you can add N days to the current date. In a BPEL process this can be useful, and you can adapt it to fit your needs. First of all, let's see how to add 1 …
This article covers the basics of the Sass, which is a CSS extension language. You will learn about variables, mixins, and nesting.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

771 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now