Page 1 of 1

Trouble with Regex

Posted: 18 Jan 2024 13:34
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 ?

Re: Trouble with Regex

Posted: 18 Jan 2024 14:12
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"

Re: Trouble with Regex

Posted: 18 Jan 2024 14:25
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 !

Re: Trouble with Regex

Posted: 18 Jan 2024 14:32
by highend
And why is it a problem if you use a following regexmatches() to get the keywords out of the previous match?

Re: Trouble with Regex

Posted: 18 Jan 2024 15:07
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.

Re: Trouble with Regex

Posted: 18 Jan 2024 15:18
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