Regex discard catch between strings

Jake

I wrote this regex: (?<![.#])\bMY_STRING\b(?![#])(?!.*(;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.

wp78de

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_STRING\1[^,]+;comment(*SKIP)(*FAIL)
|(^.*;video[ ]*$)(*SKIP)(*FAIL)

Demo

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Regex: Match all characters between two strings

分類Dev

python regex for extracting strings between certain two strings

分類Dev

Regex Find email address between First Two Instances of Strings

分類Dev

regex command line linux - select all lines between two strings

分類Dev

Regex to discard an entire capture if it's immediately preceded by a specific character

分類Dev

Replacing strings and RegEx in a file

分類Dev

regex to catch beetween tags, but not certain chatacter

分類Dev

RegEx for finding strings with chars and numbers

分類Dev

Replacing sub strings with regex in powershell

分類Dev

Regex to get string between \" and \"

分類Dev

What is the difference between (.*?) and (.*)? in regex?

分類Dev

Regex Capture between a delimiter

分類Dev

Difference between Exception and JSONException in try-catch

分類Dev

Deletion distance between 2 strings

分類Dev

Common strings between two sequences

分類Dev

javascript parse string between strings

分類Dev

Python Diferences between lines of strings

分類Dev

Regex to match anything between a pattern

分類Dev

Skip text between quotes in Regex

分類Dev

RegEx Find Text Between Dashes

分類Dev

Regex all between tag with filter

分類Dev

Regex to match everything except strings that starts with digits

分類Dev

Regex for strings containing substring that contains substring

分類Dev

regex print file extensions within strings perfectly

分類Dev

Regex that match everything except the list of strings

分類Dev

Regex to replace spaces in quoted strings with _ (Sublime)

分類Dev

Regex and strings, how do I fix a ' in a string?

分類Dev

Regex: Match if strings starts with a letter or number and then

分類Dev

How to catch words that ends with dash and its following word? Regex

Related 関連記事

  1. 1

    Regex: Match all characters between two strings

  2. 2

    python regex for extracting strings between certain two strings

  3. 3

    Regex Find email address between First Two Instances of Strings

  4. 4

    regex command line linux - select all lines between two strings

  5. 5

    Regex to discard an entire capture if it's immediately preceded by a specific character

  6. 6

    Replacing strings and RegEx in a file

  7. 7

    regex to catch beetween tags, but not certain chatacter

  8. 8

    RegEx for finding strings with chars and numbers

  9. 9

    Replacing sub strings with regex in powershell

  10. 10

    Regex to get string between \" and \"

  11. 11

    What is the difference between (.*?) and (.*)? in regex?

  12. 12

    Regex Capture between a delimiter

  13. 13

    Difference between Exception and JSONException in try-catch

  14. 14

    Deletion distance between 2 strings

  15. 15

    Common strings between two sequences

  16. 16

    javascript parse string between strings

  17. 17

    Python Diferences between lines of strings

  18. 18

    Regex to match anything between a pattern

  19. 19

    Skip text between quotes in Regex

  20. 20

    RegEx Find Text Between Dashes

  21. 21

    Regex all between tag with filter

  22. 22

    Regex to match everything except strings that starts with digits

  23. 23

    Regex for strings containing substring that contains substring

  24. 24

    regex print file extensions within strings perfectly

  25. 25

    Regex that match everything except the list of strings

  26. 26

    Regex to replace spaces in quoted strings with _ (Sublime)

  27. 27

    Regex and strings, how do I fix a ' in a string?

  28. 28

    Regex: Match if strings starts with a letter or number and then

  29. 29

    How to catch words that ends with dash and its following word? Regex

ホットタグ

アーカイブ