Skip to content
Advertisement

Regex discard catch between strings

I wrote this regex: (?<![.#])bMY_STRINGb(?![#])(?!.*(;video))

With the intention to search for a string not containing . or # preceding it, and no # succeeding, it also checks if at the ending does not contain the word ;video.

I’m trying now in how I could also check for if between the found string exist a comma before it, and the text ;comment after it, if so then don’t catch this string.

Examples:

text,MY_STRING ;comment  
text,MY_STRING text ;comment  
text,text MY_STRING text ;comment  
- > dont catch any above because exist a
**comma** before MY_STRING and ;comment after.


text, MY_STRING , text ;comment 
-> catch because exist a **comma** after MY_STRING (explanation at the bottom of thread)


text, MY_STRING, MY_STRING ;comment
-> catch first MY_STRING but don't catch last MY_STRING
because she is between , and ;comment


MY_STRING
MY_STRING,text
text, MY_STRING,
MY_STRING ;comment 
text MY_STRING ;comment 
text,MY_STRING,text ;comment
%MY_STRING% , (MY_STRING)
-> catch because there's no [comma . #] preceding MY_STRING 
and no [# ;comment ;video] succeeding


.MY_STRING
#MY_STRING
MY_STRING#
- > Independent of where MY_STRING is, either what it has between, don't catch any, because they have . or # preceding/proceeding the string.


MY_STRING, .MY_STRING, #MY_STRING, text, MY_STRING
text, MY_STRING#, MY_STRING, .MY_STRING
-> same as above, just catch MY_STRING samples not containing . #


text, MY_STRING ;video
MY_STRING ;video
#MY_STRING, MY_STRING, text, text MY_STRING ;video
- > In this case, the string will not be caught independent of where it is or what it has preceding/succeeding because exist the string ;video at the ending.


MY_STRING 
          ^ in the end line could exist only one of those: ;comment or ;video and not both at same time.

The needle regex must obey the things I’m trying to catch as described at the beginning of the thread.

Edit: added more samples on the examples above, also:

Q- Why should there be a match in text,MY_STRING,text ;comment? The MY_STRING is preceded with, and is followed with ;comment some chars after.

Because there’s a comma after the string, in this case, she can be caught, but if it was any other special char the don’t, example:

text,MY_STRING, text ;comment
              ^ ok catch (reason explained above)

text,MY_STRING@ text ;comment
              ^ dont catch, because there's no comma after the string, and there's a comma before and ;comment after

MY_STRING,text,#MY_STRING,text,text,%MY_STRING% @ $text #text <text> ;comment
^[1]             ^[2]                ^[3]

[1] catch
[2] don't (# preceding)
[3] don't catch, because there's a comma preceding, no comma succeding and at the end line exist the string ;comment

.MY_STRING,text,MY_STRING,text,text,%MY_STRING% @ $text, #text <text> ;comment
^[1]              ^[2]                ^[3]             ^[4]

[1] don't (dot preceding)
[2] catch
[3] catch because there's a comma on [4] so the string is not anymore between a comma preceding and ;comment succeeding, she is now between a comma preceding a comma succeeding and ;comment

Added the examples here: https://regex101.com/r/himCtG/2/

Guys, I know its confusing, I spent hours trying my best to make it clearer, please don’t negative my question, I really need/appreciate any help on it, if there’s something unclearer tag me and I’m asap will answer.

Advertisement

Answer

Your description is not entirely clear but I think I got the idea; how about this:

(?:^MY_STRING)|(?<=,) *MY_STRING(?= *,)|(?<=[^, ]) *MY_STRING(?= *[^,](?:;(?:comment|video)))

With the updated samples I came up with this pattern that seems to cover all cases; though I still feel not all possible permutations are covered:

(?<=^|,|w[ ])[ ]*K(?!<[^.#])MY_STRING(?=[^.#])[ ]*(?=,|$)
|(?<=%)MY_STRING(?=%)|(?<=()MY_STRING(?=))
|(?:^|w[ ]*)KMY_STRING(?=[ ]*;comment)
|,[ ]*(%)?MY_STRING1[^,]+;comment(*SKIP)(*FAIL)
|(^.*;video[ ]*$)(*SKIP)(*FAIL)

Demo

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement