This is a common pattern that I can't seem to work out.
'startpoint', followed by
any number of 'not multi letter pattern', until
'multi letter pattern'.
Here's an example:
Code: Select allstr = 'anything <startpoint>anything, including <endpoint, and more text <endpoint again more text <endpoint> anything';
With my re I would like to return the characters between and including <startpoint> and <endpoint>. If I know the string does not contain say '@' then the following works:
Code: Select allre = /<startpoint>[^@]*<endpoint>/g;
But then if the string contains @ I am done. What I really want is to replace the [^@] with any part of but not the full sequence '<endpoint>'.
It sounds like a case of using (?=<endpoint>) or (?!<endpoint>), for example something like:
Code: Select allre = /<startpoint>[^<(?=endpoint>)]*<endpoint>/g;
or
re = /<startpoint>[^<](?!<endpoint>)*<endpoint>/g;
or
re = /<startpoint>[^<]*(?!<endpoint>)*<endpoint>/g;
or any number of further variations, none of which seem to work. I assume this is not an unusual search, anyone got an answer?
Andrew
RegExp HelpMe thread
RegExp HelpMe thread
Somehow asking a question often precedes me finding the answer. Here is one solution:
re = /<startpoint>(?:a|[^a])*<endpoint>/;
Andrew
re = /<startpoint>(?:a|[^a])*<endpoint>/;
Andrew
RegExp HelpMe thread
And a better answer:
re = /<startpoint>(?:<(?!endpoint>)|[^<])*<endpoint>/;
This prevents it jumping ahead to a second endpoint.
Andrew
re = /<startpoint>(?:<(?!endpoint>)|[^<])*<endpoint>/;
This prevents it jumping ahead to a second endpoint.
Andrew
RegExp HelpMe thread
Maybe someone can help me with this. A cross platform way to edit a string read from a text file that reduces all multiple blank lines to a single blank line. A blank line can contain spaces or tabs and then has the appropriate return at the end for the platform. I have the cross-platform newline function (the replace), it's the search bit I am stuck on.
Andrew
Andrew
RegExp HelpMe thread
This removes multiple blank lines on Windows:
Code: Select allre = /\n[ \t]*(?:\n[ \t]*)+\n*/g;
newStr = str.replace(re,'\n\n');
I'll add it to the RE thread in a while but if anyone has a better suggestion or can check if this works on a Mac that would be much appreciated.
Andrew
Code: Select allre = /\n[ \t]*(?:\n[ \t]*)+\n*/g;
newStr = str.replace(re,'\n\n');
I'll add it to the RE thread in a while but if anyone has a better suggestion or can check if this works on a Mac that would be much appreciated.
Andrew