Trouble with Regex

What other productivity software are you working with...
Post Reply
GreetingsFromPoland
Posts: 224
Joined: 29 Sep 2022 14:20
Location: Win10 @125%

Trouble with Regex

Post by GreetingsFromPoland »

hi! i often wish there was an off-topic section here as the wealth of knowledge outside of XY from our community is tremendous. i have exhausted my searching and testing and have quite frankly given up.

i have spent the better half of the last 2 days trying to create/solve a regex pattern. i can get the individual "parts" working relatively easy; however, not combined. i also refuse to "ask" some AI site for help and honestly, the toxicity of other services and forums is too much.

i am trying to match a set of keywords only inside of double-quotes. i can match the keywords and i can match the quoted text, but just combined.

example keywords:
TEST SAMPLE DEPOSIT

example string:
this is a TEST for matching "keywords like TEST and SAMPLE inside of quotes" but not this TEST and DEPOSIT because they are outside

attempting to match:
this is a TEST for matching "keywords like TEST and SAMPLE inside" of quotes but not TEST and DEPOSIT because they are outside

would it be possible to create an technical-only off-topic section only available to logged-in members ?

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

Re: Trouble with Regex

Post by highend »

Code: Select all

    $example = <<<>>>
this is a TEST for matching "keywords like TEST and SAMPLE inside of quotes" but not this TEST and DEPOSIT because they are outside
    >>>;
    $keywords = "TEST|SAMPLE|DEPOSIT";
    $match    = regexmatches($example, "[""][^""]*?" . "($keywords)" . "[^""]*?[""]", <crlf>);
    text $match; // "keywords like TEST and SAMPLE inside of quotes"
One of my scripts helped you out? Please donate via Paypal

GreetingsFromPoland
Posts: 224
Joined: 29 Sep 2022 14:20
Location: Win10 @125%

Re: Trouble with Regex

Post by GreetingsFromPoland »

thank you! i don't think my attempt at formatting worked in my post. i tried to bold only what i was trying to get.

i did get your exact result; however, i am trying to only get the keywords inside the quotes and not the fully quoted text.
so instead of this :

Code: Select all

text $match; // "keywords like TEST and SAMPLE inside of quotes"
i am trying for this :

Code: Select all

text $match; // TEST SAMPLE
(only the found ones inside quotes, ignore other quoted text and outside-the-quotes matched keywords).

this is a tricky one !

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

Re: Trouble with Regex

Post by highend »

And why is it a problem if you use a following regexmatches() to get the keywords out of the previous match?
One of my scripts helped you out? Please donate via Paypal

GreetingsFromPoland
Posts: 224
Joined: 29 Sep 2022 14:20
Location: Win10 @125%

Re: Trouble with Regex

Post by GreetingsFromPoland »

highend wrote: 18 Jan 2024 14:32 And why is it a problem if you use a following regexmatches() to get the keywords out of the previous match?
it's not a problem and i can do that.
i think i was focused on attempting to extract/identify the keywords in a single statement/pattern.

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

Re: Trouble with Regex

Post by highend »

I don't think that this is solvable in a single pass (in that case via regexreplace()) because it would stop after finding the first occurrence of any of the three test keywords
One of my scripts helped you out? Please donate via Paypal

Post Reply