6 Replies Latest reply: Apr 28, 2012 11:39 AM by Muppet Mark RSS

    Rex Exp help

    Muppet Mark Community Member

      Im trying to find a reg exp that will match everything between 2 string matches… The two strings themselves are not a problem but all between is… I can't use the multiline /m flag or can I? I have seen using [\s\S] but this don't appear to work for me? I would have never thought of that… So this is the start of what Im after… and it works just fine…

       

      info = str.match( /%!PS-Adobe-3\.0/ );

       

      This is the end match…

       

      info = str.match( /%%EndComments/ );

       

      Both alone work and the match is not null…

       

      The closest I've been able to manage myself is…

       

      info = str.match( /%!PS-Adobe-3\.0[\s\n\r]+.+/ );

       

      But I'll be shot if I can find a way to get the latter part to repeat? Is it easy or do I got look at string methods instead… Oh btw can you get the offsets from an exp match? Mood swings or what?

       

      Edit… Hum Played a little longer and I now have this but it's sloooooow is that down to a poor exp and is there a cleaner method of doing the same…

       

       

      info = str.match( /%!PS-Adobe-3\.0(.+[\s\n\r]+){0,}%%EndComments/ ); Getting a little happier now…

        • 1. Re: Rex Exp help
          Muppet Mark Community Member

          A lesson learned I thinks… Two indexOf() and an slice( a, b ); was way way faster for all I wanted… Sorry for troubling you's…

          • 2. Re: Rex Exp help
            Muppet Mark Community Member

            Can I really give myself points for that? I don't think so… Should be shot for not tring it first…

            • 3. Re: Rex Exp help
              Paul Riggott Community Member

              Would something like this work for you Mark?

               

               

              var str="!PS-Adobe-3.0This is the text I want%EndComments";
              alert(str.match(/Adobe-3.0[^%]*/).toString().substr(9));
              
              

               

               

              Or even this...

               

               

              var str="!PS-Adobe-3.0This is the text I want%EndComments!\r\r\r";
              str +="<fsgfxjsdcjsdcj>PS-Adobe-3.0This is the second lot of text I want%EndComments";
              var parts = str.match(/Adobe-3.0(.*?)%EndComments/g);
              for(var a=0;a<parts.length;a++){
              alert(parts[a].toString().match(/Adobe-3.0(.*?)%EndComments/)[1]);
              }
              
              
              • 4. Re: Rex Exp help
                Muppet Mark Community Member

                Paul, I will take a look at those when I get the time… ( see if I can work the methods out ) My stupid muppet issue was treating this whole thing as variable text but its NOT start and end are constants and I can get all between… This is much faster… 30 seconds with reg exp milliseconds with indexOf I can then use rex exp on a vastly smaller chunk of data… I've meanwhlie shot myself in the head… 6 times to no effect… who said happiness is a warm gun

                • 5. Re: Rex Exp help
                  Michael L Hale Community Member

                  Paul posted the RegExp I was going to suggest.

                   

                  As far as match indexes go if you use exec() you can get the index of the match

                   

                   

                  var str="!PS-Adobe-3.0This is the text I want%EndComments!/r/r/r";
                  str +="<fsgfxjsdcjsdcj>PS-Adobe-3.0This is the second lot of text I want%EndComments";
                  var RE = /Adobe-3.0(.*?)%EndComments/g;
                  var match = null;
                  while( match = RE.exec(str) ){
                      alert(match.index);
                  }
                  
                  • 6. Re: Rex Exp help
                    Muppet Mark Community Member

                    Paul, Isn't live boring when your always right? Mike that I don't recall seeing one before thanks, I would never have worked that out from the guides… Hum a long n arduous road ahead…