Search Unity

Question Catching strings outside of quotes(Regex)

Discussion in 'Scripting' started by RoeeHerzovich, Sep 19, 2020.

  1. RoeeHerzovich

    RoeeHerzovich

    Joined:
    Apr 12, 2020
    Posts:
    103
    I wanna catch strings outside of quotes using regex.
    For example:
    I wanna catch that "But not that" and I want that one too!

    So far I've gotten to that:
    (?<!"\S*|'\S*)(?!\S*"|\S*')[a-zA-Z]

    it catches all CHARACTERS outside of quotes, meaning that if I do:

    Hello there "how are you"
    it will not catch hello world but will catch
    H e l l o t h e r e
    anyone got any idea how to make it catch the entire string?

    I appreciate the help :)
     
  2. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,605
    I cant help with regex, but if other solutions are allows this should do the same:
    Add a token string to the beginning of your string. Split() the string at quotation marks. For each uneven index of the resulting array, clean the contents to an empty string. Now remove the token string from the first index again and rebuild the string. The result should be the same string minus anything in quotation marks.

    The token string only exists to guarantee that the string does not start with a quotation mark. So it makes sure that the text in quotes always ends up on uneven indices. The token can be anything that does not normally appear in the string.
    May break on unusual usage of quotation marks tho, as it assumes that the unwanted text is always properly encapsulated with one quotation mark in front and after, and no others occuring in the string.
     
  3. RoeeHerzovich

    RoeeHerzovich

    Joined:
    Apr 12, 2020
    Posts:
    103
    @Yoreki

    Thank you for your answer, however, I found a solution:

    (?<!"\S*|'\S*)(?!\S*"|\S*')\w+

    (will not work with floating numbers, it will mark both digits but not floats, however a small modification would do)