Page 3 of 7

Re: A bit of regex help needed

Posted: 06 Jan 2013 15:17
by SkyFrontier
For
Start: "(NEXT 5 (good)"
End: "Closing Times"

I need to remove everything that's between 'start' and 'end', including 'start' and 'end'.

$a = regexreplace("<clipboard>", "\(NEXT 5 (good)\).*?\(Closing Times\)", ""); echo $a;

Is not working.

Any help here, please?

Re: A bit of regex help needed

Posted: 06 Jan 2013 15:31
by highend

Code: Select all

    $c = <<<>>>
(NEXT 5 (good)
This is one sentence.
Ok, another one...
(Closing Times)
>>>;

    $a = regexreplace($c, "\(NEXT 5 \(good\)[\s\S]*?\(Closing Times\)", ""); echo $a;
1.

Code: Select all

"\(NEXT 5 (good)\)
It doesn't match (NEXT 5 (good)

You've created a reference group only for "good". The parenthesis must be escaped.
And the "\)" => There is no (second) closing parenthesis in your clipboard after "good)"...

My regex works for text with and without line breaks.

Re: A bit of regex help needed

Posted: 06 Jan 2013 16:20
by SkyFrontier
"Closing Times" has no parenthesis, so I finally discovered that this was preventing the actual job to work properly.

Code: Select all

    $c = <<<>>>
This will be kept!
(NEXT 5 (good)
This is one sentence.
Ok, another one...
Closing Times

...this will remain too!
>>>;

  $a = regexreplace($c, "\(NEXT 5 \(good\)[\s\S]*?\Closing Times\r\n[\s\S]", ""); echo $a;
should do as per your code but it's not. How do I fix it?

Re: A bit of regex help needed

Posted: 06 Jan 2013 16:37
by highend
Because you have a backslash, that doesn't belong there.

Code: Select all

\Closing

Re: A bit of regex help needed

Posted: 06 Jan 2013 16:42
by SkyFrontier
Parfait!
Thank you, highend!

Re: A bit of regex help needed

Posted: 23 Mar 2014 14:05
by SkyFrontier
Guys,

I need to fix indentation for .xys files.
Is this safe? (please see the comment-question on the code, too)

Code: Select all

   $re = regexreplace("$re", '\r\n"', '"'); //just in case
   $re = regexreplace("$re", '(\r\n)([0-9])', '$2');
   $re = regexreplace("$re", '(\r\n)([a-zA-Z])', '$2');
   $re = regexreplace("$re", '(\r\n)([\s\S])', '$2'); // is this valid for SYMBOLS, ie, non-numbers && non-letters?
Thanks in advance.

Re: A bit of regex help needed

Posted: 23 Mar 2014 18:18
by Marco
No, it's not. [\s\S] is like a dot that matches new lines too. Try

Code: Select all

\W
EDIT: ok, maybe I got it wrong. What do you want to match exactly?

Re: A bit of regex help needed

Posted: 23 Mar 2014 19:07
by SkyFrontier
I want to match stuff like

pseudo"123
"this;
-making
pseudo"123"this;

pseudo"123
abc"this;
-making
pseudo"123abc"this;

pseudo"
123"this;
-making
pseudo"123"this;

pseudo"
&123"this;
-making
pseudo"&123"this;

Thanks, Marco.

Re: A bit of regex help needed

Posted: 23 Mar 2014 19:33
by Marco
How many \r\n are there between two quotes?

Re: A bit of regex help needed

Posted: 23 Mar 2014 20:05
by SkyFrontier
A single <crlf> separating the two lines that may join into one.

Re: A bit of regex help needed

Posted: 23 Mar 2014 20:11
by Marco

Code: Select all

"(.*?)\r\n(.*?)"

Re: A bit of regex help needed

Posted: 24 Mar 2014 01:06
by SkyFrontier
$re = regexreplace("$re", '\r\n"', '"');
$re = regexreplace("$re", '(\r\n)([a-zA-Z0-9])', '$2');
$re = regexreplace("$re", '(\r\n)([^0-9a-zA-Z\s])', '$2');

Your code is matching everything as it seems, so in a regexreplace all is gone. Those codes I'm using are doing the job. Anything potentially harmful on them? Now that you may have a clearer picture, any elegant solution?

Re: A bit of regex help needed

Posted: 24 Mar 2014 01:13
by Marco
It shouldn't match everything, only the text between two quotes. Tested it in a text editor with your test cases and it worked perfectly :?

Re: A bit of regex help needed

Posted: 24 Mar 2014 13:10
by Marco
Lol I'm an idiot, I gave you the matching but not the replacement :oops:

Code: Select all

$re = regexreplace("$re", '"(.*?)\r\n(.*?)"', '"$1$2"');
Input:

Code: Select all

 $re =<<<QWERTYUIOP
pseudo"123
"this;

pseudo"123
abc"this;

pseudo"
123"this;

pseudo"
&123"this;
QWERTYUIOP;

 $re = regexreplace("$re", '"(.*?)\r\n(.*?)"', '"$1$2"');

 text $re;
Output:

Code: Select all

pseudo"123"this;

pseudo"123abc"this;

pseudo"123"this;

pseudo"&123"this;

Re: A bit of regex help needed

Posted: 24 Mar 2014 16:43
by SkyFrontier
No, Marco - I AM the idiot here as I DID used the "$1$2" (also $1, also $2, also "" and so on...) and got nothing.
Strange.
Anyway, now it's working and I'll stick to your solution as it seems more reliable. Hey, total regex-n00b here! (Last year I tried to get some regex lessons but life had another turn on me = pending!)

Many thanks.