Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #91250

Re: [OT] Help for a RegEx

Newsgroups comp.lang.c++
Date 2023-08-12 23:33 -0700
References <uae4bc$6g1o$1@dont-email.me>
Message-ID <f321bcfa-a0fb-41f1-b115-df15744db44cn@googlegroups.com> (permalink)
Subject Re: [OT] Help for a RegEx
From wij <wyniijj5@gmail.com>

Show all headers | View raw


On Thursday, August 3, 2023 at 1:39:17 AM UTC+8, Fonntuggnio wrote:
> Sorry for the total OT, but I failed to build a RegEx with 
> the "help" (rotfl) of three different so called IA, getting 
> to nothing 
> 
> I am scanning an HTML document (not in javascript, so I do 
> not have access to DOM nodes from inside) and I need to 
> match EVERY <p> whole tag. 
> 
> for whole I mean, starting from the <p and ending with the 
> corresponding </p>, but such paragrapha MAY (and may not) 
> contain 
> 
> a long list of attributes, with or without zero or more \n 
> \r \t characters, valid, before the >. 

The following is an example program of class Regex (class wrapper of regex(3)
functions). The regular expression "<p>.*</p>" should do most of the job, except
real HTML involves comments, nested tags, erroneous format...etc:

[]a_grep "<p>.*</p>" *html

-------------------------------------------------
/* Copyright is licensed by GNU LGPL, see file COPYING.       by I.J.Wang 2023

  Simulate grep command (Extended regular expression, ERE)

  Build: make a_grep
*/
#include <Wy.stdio.h>
#include <Wy.unistd.h>
#include <Wy.regex.h>

using namespace Wy;

constexpr const char Red[]="\x1B[31m";
constexpr const char Reset[]= "\x1B[0m";

void sim_grep(Regex& rexpr, const char* fname)
{
 Errno r;
 String str;
 ::regmatch_t mbuf[5];
 RegFile regf(fname,O_RDONLY);
 RdBuf strm(regf);

 for(;strm.is_eof()==false;) {
   if((r=strm.read(str))!=Ok) {
     WY_THROW(r);
   }
   if((r=rexpr.regexec(str.c_str(),mbuf,WY_CARR_SIZE(mbuf),0))!=Ok) {
     continue;
   }
   cout << fname << ": ";
   cout << StrSeg(str.begin(), str.begin()+mbuf[0].rm_so);
   cout << Red << StrSeg(str.begin()+mbuf[0].rm_so,
                         str.begin()+mbuf[0].rm_eo) << Reset;
   cout << StrSeg(str.begin()+mbuf[0].rm_eo, str.end());
 }
};

int main(int argc, const char* argv[])
try {
 static const char usage[]="a_grep <pattern> <file>+" WY_ENDL;
 Errno r;

 if(argc<3) {
   cout << "Error: Invalid argument" WY_ENDL "Usage: "
        << usage << WY_ENDL;
   return -1;
 }
 const char* ptn= argv[1];
 Regex rexpr;

 if((r=rexpr.regcomp(ptn,REG_EXTENDED))!=Ok) {
   if(r!=EBADMSG) {
     WY_THROW(r);
   }
   String str;
   if((r=rexpr.regerror(str))!=Ok) {
     WY_THROW(r);
   }
   cout << str << WY_ENDL;
   return -1;
 }

 for(int i=2; i<argc; ++i) {
   const char* fname= argv[i];
   sim_grep(rexpr,fname);
 }

 cout << "OK" WY_ENDL;
 return 0;
}
catch(const Errno& e) {
 cerr << wrd(e) << WY_ENDL;
 return -1;
}
catch(...) {
 cerr << "main() caught(...)" WY_ENDL;
 throw;
};

Back to comp.lang.c++ | Previous | Next — Previous in thread | Find similar | Unroll thread


Thread

[OT] Help for a RegEx Fonntuggnio <JoeFonntuggnio@libbbero.it> - 2023-08-02 19:38 +0200
  Re: [OT] Help for a RegEx Christian Gollwitzer <auriocus@gmx.de> - 2023-08-02 20:00 +0200
    Re: [OT] Help for a RegEx MarioCPPP <NoliMihiFrangereMentulam@libero.it> - 2023-08-03 00:52 +0200
  Re: [OT] Help for a RegEx Paavo Helde <eesnimi@osa.pri.ee> - 2023-08-02 21:15 +0300
    Re: [OT] Help for a RegEx scott@slp53.sl.home (Scott Lurndal) - 2023-08-02 18:20 +0000
      Re: [OT] Help for a RegEx MarioCPPP <NoliMihiFrangereMentulam@libero.it> - 2023-08-03 00:56 +0200
    Re: [OT] Help for a RegEx MarioCPPP <NoliMihiFrangereMentulam@libero.it> - 2023-08-03 00:55 +0200
      Re: [OT] Help for a RegEx Paavo Helde <eesnimi@osa.pri.ee> - 2023-08-03 11:36 +0300
        Re: [OT] Help for a RegEx MarioCPPP <NoliMihiFrangereMentulam@libero.it> - 2023-08-04 02:01 +0200
          Re: [OT] Help for a RegEx Ben Bacarisse <ben.usenet@bsb.me.uk> - 2023-08-04 01:26 +0100
            Re: [OT] Help for a RegEx MarioCPPP <NoliMihiFrangereMentulam@libero.it> - 2023-08-05 02:17 +0200
              Re: [OT] Help for a RegEx jak <nospam@please.ty> - 2023-08-14 10:37 +0200
                Re: [OT] Help for a RegEx MarioCCCP <NoliMihiFrangereMentulam@libero.it> - 2023-08-15 14:14 +0200
                Re: [OT] Help for a RegEx jak <nospam@please.ty> - 2023-08-16 21:24 +0200
                Re: [OT] Help for a RegEx MarioCCCP <NoliMihiFrangereMentulam@libero.it> - 2023-08-18 21:05 +0200
                Re: [OT] Help for a RegEx jak <nospam@please.ty> - 2023-08-19 09:18 +0200
  Re: [OT] Help for a RegEx Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2023-08-02 11:58 -0700
  Re: [OT] Help for a RegEx wij <wyniijj5@gmail.com> - 2023-08-12 23:33 -0700

csiph-web