A bit of regex help needed

Please check the FAQ (https://www.xyplorer.com/faq.php) before posting a question...
SkyFrontier
Posts: 2341
Joined: 04 Jan 2010 14:27
Location: Pasárgada (eu vou!)

Re: A bit of regex help needed

Post 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?
New User's Ref. Guide and Quick Setup Guide can help a bit! Check XYplorer Resources Index for many useful links!
Want a new XYperience? XY MOD - surfYnXoard
-coz' the aim of computing is to free us to LIVE...

highend
Posts: 13313
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: A bit of regex help needed

Post 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.
One of my scripts helped you out? Please donate via Paypal

SkyFrontier
Posts: 2341
Joined: 04 Jan 2010 14:27
Location: Pasárgada (eu vou!)

Re: A bit of regex help needed

Post 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?
New User's Ref. Guide and Quick Setup Guide can help a bit! Check XYplorer Resources Index for many useful links!
Want a new XYperience? XY MOD - surfYnXoard
-coz' the aim of computing is to free us to LIVE...

highend
Posts: 13313
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: A bit of regex help needed

Post by highend »

Because you have a backslash, that doesn't belong there.

Code: Select all

\Closing
One of my scripts helped you out? Please donate via Paypal

SkyFrontier
Posts: 2341
Joined: 04 Jan 2010 14:27
Location: Pasárgada (eu vou!)

Re: A bit of regex help needed

Post by SkyFrontier »

Parfait!
Thank you, highend!
New User's Ref. Guide and Quick Setup Guide can help a bit! Check XYplorer Resources Index for many useful links!
Want a new XYperience? XY MOD - surfYnXoard
-coz' the aim of computing is to free us to LIVE...

SkyFrontier
Posts: 2341
Joined: 04 Jan 2010 14:27
Location: Pasárgada (eu vou!)

Re: A bit of regex help needed

Post 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.
New User's Ref. Guide and Quick Setup Guide can help a bit! Check XYplorer Resources Index for many useful links!
Want a new XYperience? XY MOD - surfYnXoard
-coz' the aim of computing is to free us to LIVE...

Marco
Posts: 2347
Joined: 27 Jun 2011 15:20

Re: A bit of regex help needed

Post 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?
Tag Backup - SimpleUpdater - XYplorer Messenger - The Unofficial XYplorer Archive - Everything in XYplorer
Don sees all [cit. from viewtopic.php?p=124094#p124094]

SkyFrontier
Posts: 2341
Joined: 04 Jan 2010 14:27
Location: Pasárgada (eu vou!)

Re: A bit of regex help needed

Post 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.
New User's Ref. Guide and Quick Setup Guide can help a bit! Check XYplorer Resources Index for many useful links!
Want a new XYperience? XY MOD - surfYnXoard
-coz' the aim of computing is to free us to LIVE...

Marco
Posts: 2347
Joined: 27 Jun 2011 15:20

Re: A bit of regex help needed

Post by Marco »

How many \r\n are there between two quotes?
Tag Backup - SimpleUpdater - XYplorer Messenger - The Unofficial XYplorer Archive - Everything in XYplorer
Don sees all [cit. from viewtopic.php?p=124094#p124094]

SkyFrontier
Posts: 2341
Joined: 04 Jan 2010 14:27
Location: Pasárgada (eu vou!)

Re: A bit of regex help needed

Post by SkyFrontier »

A single <crlf> separating the two lines that may join into one.
New User's Ref. Guide and Quick Setup Guide can help a bit! Check XYplorer Resources Index for many useful links!
Want a new XYperience? XY MOD - surfYnXoard
-coz' the aim of computing is to free us to LIVE...

Marco
Posts: 2347
Joined: 27 Jun 2011 15:20

Re: A bit of regex help needed

Post by Marco »

Code: Select all

"(.*?)\r\n(.*?)"
Tag Backup - SimpleUpdater - XYplorer Messenger - The Unofficial XYplorer Archive - Everything in XYplorer
Don sees all [cit. from viewtopic.php?p=124094#p124094]

SkyFrontier
Posts: 2341
Joined: 04 Jan 2010 14:27
Location: Pasárgada (eu vou!)

Re: A bit of regex help needed

Post 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?
New User's Ref. Guide and Quick Setup Guide can help a bit! Check XYplorer Resources Index for many useful links!
Want a new XYperience? XY MOD - surfYnXoard
-coz' the aim of computing is to free us to LIVE...

Marco
Posts: 2347
Joined: 27 Jun 2011 15:20

Re: A bit of regex help needed

Post 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 :?
Tag Backup - SimpleUpdater - XYplorer Messenger - The Unofficial XYplorer Archive - Everything in XYplorer
Don sees all [cit. from viewtopic.php?p=124094#p124094]

Marco
Posts: 2347
Joined: 27 Jun 2011 15:20

Re: A bit of regex help needed

Post 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;
Tag Backup - SimpleUpdater - XYplorer Messenger - The Unofficial XYplorer Archive - Everything in XYplorer
Don sees all [cit. from viewtopic.php?p=124094#p124094]

SkyFrontier
Posts: 2341
Joined: 04 Jan 2010 14:27
Location: Pasárgada (eu vou!)

Re: A bit of regex help needed

Post 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.
New User's Ref. Guide and Quick Setup Guide can help a bit! Check XYplorer Resources Index for many useful links!
Want a new XYperience? XY MOD - surfYnXoard
-coz' the aim of computing is to free us to LIVE...

Post Reply