Regex pattern to produce appropriate escape character behavior












-1















I'm making a Sublime Text 3 language definition for syntax highlighting. The language is a simple 'expression language' (nothing to do with Java) that is used to display information from a media library on a display panel. The purpose of the syntax highlighting is to clarify exactly what will be displayed on the panel.



Accordingly, I would like to explicate exactly what a sequence of escape characters will look like when interpreted and shown on the display panel. The escape character is a forward-slash, and if you would like to display a literal forward-slash, you have to escape it with another. Therefore, a series of forward-slashes will result in every-other forward-slash being displayed on the panel. Additionally, if there is a special character after a forward-slash, it will be escaped - but if a special character is placed after TWO (or four, or six...) forward slashes, it will not be escaped, because the escape characters will cancel each other out.



The highlighting I would like to achieve (with example special character 's') is as follows:



Desired Highlighting



I have to achieve this simply by feeding Sublime Text regular expressions, in YAML format, so the solution has to be one made purely of regex.



Is this possible? If so, how can I achieve it?










share|improve this question

























  • You don't want an odd number of slashes. You want pairs of slashes to become a literal slash, and after that, single slashes are escape chars.

    – Bohemian
    Nov 21 '18 at 1:11
















-1















I'm making a Sublime Text 3 language definition for syntax highlighting. The language is a simple 'expression language' (nothing to do with Java) that is used to display information from a media library on a display panel. The purpose of the syntax highlighting is to clarify exactly what will be displayed on the panel.



Accordingly, I would like to explicate exactly what a sequence of escape characters will look like when interpreted and shown on the display panel. The escape character is a forward-slash, and if you would like to display a literal forward-slash, you have to escape it with another. Therefore, a series of forward-slashes will result in every-other forward-slash being displayed on the panel. Additionally, if there is a special character after a forward-slash, it will be escaped - but if a special character is placed after TWO (or four, or six...) forward slashes, it will not be escaped, because the escape characters will cancel each other out.



The highlighting I would like to achieve (with example special character 's') is as follows:



Desired Highlighting



I have to achieve this simply by feeding Sublime Text regular expressions, in YAML format, so the solution has to be one made purely of regex.



Is this possible? If so, how can I achieve it?










share|improve this question

























  • You don't want an odd number of slashes. You want pairs of slashes to become a literal slash, and after that, single slashes are escape chars.

    – Bohemian
    Nov 21 '18 at 1:11














-1












-1








-1








I'm making a Sublime Text 3 language definition for syntax highlighting. The language is a simple 'expression language' (nothing to do with Java) that is used to display information from a media library on a display panel. The purpose of the syntax highlighting is to clarify exactly what will be displayed on the panel.



Accordingly, I would like to explicate exactly what a sequence of escape characters will look like when interpreted and shown on the display panel. The escape character is a forward-slash, and if you would like to display a literal forward-slash, you have to escape it with another. Therefore, a series of forward-slashes will result in every-other forward-slash being displayed on the panel. Additionally, if there is a special character after a forward-slash, it will be escaped - but if a special character is placed after TWO (or four, or six...) forward slashes, it will not be escaped, because the escape characters will cancel each other out.



The highlighting I would like to achieve (with example special character 's') is as follows:



Desired Highlighting



I have to achieve this simply by feeding Sublime Text regular expressions, in YAML format, so the solution has to be one made purely of regex.



Is this possible? If so, how can I achieve it?










share|improve this question
















I'm making a Sublime Text 3 language definition for syntax highlighting. The language is a simple 'expression language' (nothing to do with Java) that is used to display information from a media library on a display panel. The purpose of the syntax highlighting is to clarify exactly what will be displayed on the panel.



Accordingly, I would like to explicate exactly what a sequence of escape characters will look like when interpreted and shown on the display panel. The escape character is a forward-slash, and if you would like to display a literal forward-slash, you have to escape it with another. Therefore, a series of forward-slashes will result in every-other forward-slash being displayed on the panel. Additionally, if there is a special character after a forward-slash, it will be escaped - but if a special character is placed after TWO (or four, or six...) forward slashes, it will not be escaped, because the escape characters will cancel each other out.



The highlighting I would like to achieve (with example special character 's') is as follows:



Desired Highlighting



I have to achieve this simply by feeding Sublime Text regular expressions, in YAML format, so the solution has to be one made purely of regex.



Is this possible? If so, how can I achieve it?







regex yaml sublimetext3 syntax-highlighting






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 '18 at 4:31







Dan Luba

















asked Nov 20 '18 at 23:46









Dan LubaDan Luba

175




175













  • You don't want an odd number of slashes. You want pairs of slashes to become a literal slash, and after that, single slashes are escape chars.

    – Bohemian
    Nov 21 '18 at 1:11



















  • You don't want an odd number of slashes. You want pairs of slashes to become a literal slash, and after that, single slashes are escape chars.

    – Bohemian
    Nov 21 '18 at 1:11

















You don't want an odd number of slashes. You want pairs of slashes to become a literal slash, and after that, single slashes are escape chars.

– Bohemian
Nov 21 '18 at 1:11





You don't want an odd number of slashes. You want pairs of slashes to become a literal slash, and after that, single slashes are escape chars.

– Bohemian
Nov 21 '18 at 1:11












3 Answers
3






active

oldest

votes


















0














Finding odd numbers of slashes is not particularly useful. This is a parsing exercise of removing slashes. If the slash is followed by another slash, it's a literal slash, otherwise it's whatever follows.



But wait a minute, there's no difference there - no matter what the following char is, it's a literal, so...



Match all "slash-anychar" pairs using the regex /(.) and replace with the captured character.



In java:



str = str.replaceAll("/(.)", "$1");


In python:



str = re.sub('/(.)', '$1', str)


In perl:



$str =~ s//(.)/$1/


etc






share|improve this answer
























  • If only it were that simple! I'm in YAML so I need a purely regex solution. I should have added a YAML tag. I'm going to do that now.

    – Dan Luba
    Nov 21 '18 at 4:30











  • @dan what does YAML have to do with your question? You can’t “do” anything in YAML - it’s pure data. Please explain more in your question what you’re trying to do.

    – Bohemian
    Nov 21 '18 at 14:56











  • Perhaps saying I'm 'in YAML' was confusing, but my point is that I am not not programming anything - I am feeding instructions to a program, and those instructions are in YAML format. The instructions contain regular expressions, and if I feed in the correct regular expressions, the program will exhibit the behavior that I require of it. The program is Sublime Text 3, and the behavior is syntax highlighting. So I just need a pure regex solution to picking out the pattern described in my question.

    – Dan Luba
    Nov 21 '18 at 21:53











  • I've edited my question. Thanks.

    – Dan Luba
    Nov 21 '18 at 22:04



















0














Without more information on the structure of the syntax you currently have, it's hard to say for sure, but I think what you're looking for here is just a simple single match construct that matches the literal character / followed by any character that is allowed to be an escape character, which in your example is the character s and the character /.



You may be getting hung up on thinking that the regular expression that you're trying to create needs to find the biggest possible match containing any combination of escape sequences. However, the match rules that you provide are used as many times as needed to consume the input, so they only need to match single syntax constructs at a time.



A simple example of this in action is the following minimal syntax. The first two match constructs are just for example purposes in the image below; they implement a line comment and flagging an 's' as special when it's not escaped so that it's clearer when that is the case.



The third rule is what matches a valid escape sequence, which is a / followed by either another / (literal /) or an s (literal s).



The last rule, which is optional, matches a / character followed by anything that's not a valid escape character (including not being followed by anything) and marks it as invalid as a hint to the user that they've done something silly.



Here I've added a space character as a potentially valid escape for the purposes of invalid escape detection so that a trailing space following a bare / doesn't get the invalid scope as well. Without that, the syntax assumes you were trying to quote the space character.



%YAML 1.2
---
scope: source.example
contexts:
main:
# A simple single-line comment scope capture for nice output
- match: '(#).*n?'
captures:
0: comment.line.number-sign.example
1: punctuation.definition.comment.example
# Just so we can apply a color to the letter s to know when it's "special"
- match: 's'
scope: entity.name.special
# The set of things that can be valid escapes
- match: '/[s/]'
scope: constant.character.escape.example
# If only s and / are valid escapes, make other escapes invalid, including
# using a bare / with nothing following it.
- match: '/[^s/ ]?'
scope: invalid.illegal.escape.example


In a simple test, the results of this syntax would be:



Syntax example






share|improve this answer
























  • Thanks. I might go with this solution at a pinch... I can see now how I didn't explain myself fully, though (I'm effectively new to Stack Overflow, new to Sublime Text, New to YAML and relatively new to Regex, so I'm generally a bit flummoxed). I have revised my question to make explicit exactly what I am trying to achieve - I think that should make it clear - would you mind taking a look?

    – Dan Luba
    Nov 22 '18 at 4:35











  • For clarity, do you expect that when you enter some text in the file, the syntax highlight is also going to modify the text to remove some of the / characters, etc?

    – OdatNurd
    Nov 22 '18 at 6:48











  • No. No modification of text, just the highlighting as shown, to indicate what WILL BE removed by the expression interpreter of the display.

    – Dan Luba
    Nov 22 '18 at 7:31











  • You see how the grey slashes are easy to ignore and so make it much easier to visualize what the display will look like after the interpreter has done its work?

    – Dan Luba
    Nov 22 '18 at 7:53



















0














Alright, so for what it's worth, I figured it out. This:



%YAML 1.2
---

file_extensions:
- example

scope: source.example

contexts:
main:
# special color setup #

- match: s
scope: green


# desired behaviour #

- match: '(/)(.)?'
captures:
1: grey
2: white


Gives you this:



Appropriate representation of escape character behaviour






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53403301%2fregex-pattern-to-produce-appropriate-escape-character-behavior%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    Finding odd numbers of slashes is not particularly useful. This is a parsing exercise of removing slashes. If the slash is followed by another slash, it's a literal slash, otherwise it's whatever follows.



    But wait a minute, there's no difference there - no matter what the following char is, it's a literal, so...



    Match all "slash-anychar" pairs using the regex /(.) and replace with the captured character.



    In java:



    str = str.replaceAll("/(.)", "$1");


    In python:



    str = re.sub('/(.)', '$1', str)


    In perl:



    $str =~ s//(.)/$1/


    etc






    share|improve this answer
























    • If only it were that simple! I'm in YAML so I need a purely regex solution. I should have added a YAML tag. I'm going to do that now.

      – Dan Luba
      Nov 21 '18 at 4:30











    • @dan what does YAML have to do with your question? You can’t “do” anything in YAML - it’s pure data. Please explain more in your question what you’re trying to do.

      – Bohemian
      Nov 21 '18 at 14:56











    • Perhaps saying I'm 'in YAML' was confusing, but my point is that I am not not programming anything - I am feeding instructions to a program, and those instructions are in YAML format. The instructions contain regular expressions, and if I feed in the correct regular expressions, the program will exhibit the behavior that I require of it. The program is Sublime Text 3, and the behavior is syntax highlighting. So I just need a pure regex solution to picking out the pattern described in my question.

      – Dan Luba
      Nov 21 '18 at 21:53











    • I've edited my question. Thanks.

      – Dan Luba
      Nov 21 '18 at 22:04
















    0














    Finding odd numbers of slashes is not particularly useful. This is a parsing exercise of removing slashes. If the slash is followed by another slash, it's a literal slash, otherwise it's whatever follows.



    But wait a minute, there's no difference there - no matter what the following char is, it's a literal, so...



    Match all "slash-anychar" pairs using the regex /(.) and replace with the captured character.



    In java:



    str = str.replaceAll("/(.)", "$1");


    In python:



    str = re.sub('/(.)', '$1', str)


    In perl:



    $str =~ s//(.)/$1/


    etc






    share|improve this answer
























    • If only it were that simple! I'm in YAML so I need a purely regex solution. I should have added a YAML tag. I'm going to do that now.

      – Dan Luba
      Nov 21 '18 at 4:30











    • @dan what does YAML have to do with your question? You can’t “do” anything in YAML - it’s pure data. Please explain more in your question what you’re trying to do.

      – Bohemian
      Nov 21 '18 at 14:56











    • Perhaps saying I'm 'in YAML' was confusing, but my point is that I am not not programming anything - I am feeding instructions to a program, and those instructions are in YAML format. The instructions contain regular expressions, and if I feed in the correct regular expressions, the program will exhibit the behavior that I require of it. The program is Sublime Text 3, and the behavior is syntax highlighting. So I just need a pure regex solution to picking out the pattern described in my question.

      – Dan Luba
      Nov 21 '18 at 21:53











    • I've edited my question. Thanks.

      – Dan Luba
      Nov 21 '18 at 22:04














    0












    0








    0







    Finding odd numbers of slashes is not particularly useful. This is a parsing exercise of removing slashes. If the slash is followed by another slash, it's a literal slash, otherwise it's whatever follows.



    But wait a minute, there's no difference there - no matter what the following char is, it's a literal, so...



    Match all "slash-anychar" pairs using the regex /(.) and replace with the captured character.



    In java:



    str = str.replaceAll("/(.)", "$1");


    In python:



    str = re.sub('/(.)', '$1', str)


    In perl:



    $str =~ s//(.)/$1/


    etc






    share|improve this answer













    Finding odd numbers of slashes is not particularly useful. This is a parsing exercise of removing slashes. If the slash is followed by another slash, it's a literal slash, otherwise it's whatever follows.



    But wait a minute, there's no difference there - no matter what the following char is, it's a literal, so...



    Match all "slash-anychar" pairs using the regex /(.) and replace with the captured character.



    In java:



    str = str.replaceAll("/(.)", "$1");


    In python:



    str = re.sub('/(.)', '$1', str)


    In perl:



    $str =~ s//(.)/$1/


    etc







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 21 '18 at 1:17









    BohemianBohemian

    298k65427561




    298k65427561













    • If only it were that simple! I'm in YAML so I need a purely regex solution. I should have added a YAML tag. I'm going to do that now.

      – Dan Luba
      Nov 21 '18 at 4:30











    • @dan what does YAML have to do with your question? You can’t “do” anything in YAML - it’s pure data. Please explain more in your question what you’re trying to do.

      – Bohemian
      Nov 21 '18 at 14:56











    • Perhaps saying I'm 'in YAML' was confusing, but my point is that I am not not programming anything - I am feeding instructions to a program, and those instructions are in YAML format. The instructions contain regular expressions, and if I feed in the correct regular expressions, the program will exhibit the behavior that I require of it. The program is Sublime Text 3, and the behavior is syntax highlighting. So I just need a pure regex solution to picking out the pattern described in my question.

      – Dan Luba
      Nov 21 '18 at 21:53











    • I've edited my question. Thanks.

      – Dan Luba
      Nov 21 '18 at 22:04



















    • If only it were that simple! I'm in YAML so I need a purely regex solution. I should have added a YAML tag. I'm going to do that now.

      – Dan Luba
      Nov 21 '18 at 4:30











    • @dan what does YAML have to do with your question? You can’t “do” anything in YAML - it’s pure data. Please explain more in your question what you’re trying to do.

      – Bohemian
      Nov 21 '18 at 14:56











    • Perhaps saying I'm 'in YAML' was confusing, but my point is that I am not not programming anything - I am feeding instructions to a program, and those instructions are in YAML format. The instructions contain regular expressions, and if I feed in the correct regular expressions, the program will exhibit the behavior that I require of it. The program is Sublime Text 3, and the behavior is syntax highlighting. So I just need a pure regex solution to picking out the pattern described in my question.

      – Dan Luba
      Nov 21 '18 at 21:53











    • I've edited my question. Thanks.

      – Dan Luba
      Nov 21 '18 at 22:04

















    If only it were that simple! I'm in YAML so I need a purely regex solution. I should have added a YAML tag. I'm going to do that now.

    – Dan Luba
    Nov 21 '18 at 4:30





    If only it were that simple! I'm in YAML so I need a purely regex solution. I should have added a YAML tag. I'm going to do that now.

    – Dan Luba
    Nov 21 '18 at 4:30













    @dan what does YAML have to do with your question? You can’t “do” anything in YAML - it’s pure data. Please explain more in your question what you’re trying to do.

    – Bohemian
    Nov 21 '18 at 14:56





    @dan what does YAML have to do with your question? You can’t “do” anything in YAML - it’s pure data. Please explain more in your question what you’re trying to do.

    – Bohemian
    Nov 21 '18 at 14:56













    Perhaps saying I'm 'in YAML' was confusing, but my point is that I am not not programming anything - I am feeding instructions to a program, and those instructions are in YAML format. The instructions contain regular expressions, and if I feed in the correct regular expressions, the program will exhibit the behavior that I require of it. The program is Sublime Text 3, and the behavior is syntax highlighting. So I just need a pure regex solution to picking out the pattern described in my question.

    – Dan Luba
    Nov 21 '18 at 21:53





    Perhaps saying I'm 'in YAML' was confusing, but my point is that I am not not programming anything - I am feeding instructions to a program, and those instructions are in YAML format. The instructions contain regular expressions, and if I feed in the correct regular expressions, the program will exhibit the behavior that I require of it. The program is Sublime Text 3, and the behavior is syntax highlighting. So I just need a pure regex solution to picking out the pattern described in my question.

    – Dan Luba
    Nov 21 '18 at 21:53













    I've edited my question. Thanks.

    – Dan Luba
    Nov 21 '18 at 22:04





    I've edited my question. Thanks.

    – Dan Luba
    Nov 21 '18 at 22:04













    0














    Without more information on the structure of the syntax you currently have, it's hard to say for sure, but I think what you're looking for here is just a simple single match construct that matches the literal character / followed by any character that is allowed to be an escape character, which in your example is the character s and the character /.



    You may be getting hung up on thinking that the regular expression that you're trying to create needs to find the biggest possible match containing any combination of escape sequences. However, the match rules that you provide are used as many times as needed to consume the input, so they only need to match single syntax constructs at a time.



    A simple example of this in action is the following minimal syntax. The first two match constructs are just for example purposes in the image below; they implement a line comment and flagging an 's' as special when it's not escaped so that it's clearer when that is the case.



    The third rule is what matches a valid escape sequence, which is a / followed by either another / (literal /) or an s (literal s).



    The last rule, which is optional, matches a / character followed by anything that's not a valid escape character (including not being followed by anything) and marks it as invalid as a hint to the user that they've done something silly.



    Here I've added a space character as a potentially valid escape for the purposes of invalid escape detection so that a trailing space following a bare / doesn't get the invalid scope as well. Without that, the syntax assumes you were trying to quote the space character.



    %YAML 1.2
    ---
    scope: source.example
    contexts:
    main:
    # A simple single-line comment scope capture for nice output
    - match: '(#).*n?'
    captures:
    0: comment.line.number-sign.example
    1: punctuation.definition.comment.example
    # Just so we can apply a color to the letter s to know when it's "special"
    - match: 's'
    scope: entity.name.special
    # The set of things that can be valid escapes
    - match: '/[s/]'
    scope: constant.character.escape.example
    # If only s and / are valid escapes, make other escapes invalid, including
    # using a bare / with nothing following it.
    - match: '/[^s/ ]?'
    scope: invalid.illegal.escape.example


    In a simple test, the results of this syntax would be:



    Syntax example






    share|improve this answer
























    • Thanks. I might go with this solution at a pinch... I can see now how I didn't explain myself fully, though (I'm effectively new to Stack Overflow, new to Sublime Text, New to YAML and relatively new to Regex, so I'm generally a bit flummoxed). I have revised my question to make explicit exactly what I am trying to achieve - I think that should make it clear - would you mind taking a look?

      – Dan Luba
      Nov 22 '18 at 4:35











    • For clarity, do you expect that when you enter some text in the file, the syntax highlight is also going to modify the text to remove some of the / characters, etc?

      – OdatNurd
      Nov 22 '18 at 6:48











    • No. No modification of text, just the highlighting as shown, to indicate what WILL BE removed by the expression interpreter of the display.

      – Dan Luba
      Nov 22 '18 at 7:31











    • You see how the grey slashes are easy to ignore and so make it much easier to visualize what the display will look like after the interpreter has done its work?

      – Dan Luba
      Nov 22 '18 at 7:53
















    0














    Without more information on the structure of the syntax you currently have, it's hard to say for sure, but I think what you're looking for here is just a simple single match construct that matches the literal character / followed by any character that is allowed to be an escape character, which in your example is the character s and the character /.



    You may be getting hung up on thinking that the regular expression that you're trying to create needs to find the biggest possible match containing any combination of escape sequences. However, the match rules that you provide are used as many times as needed to consume the input, so they only need to match single syntax constructs at a time.



    A simple example of this in action is the following minimal syntax. The first two match constructs are just for example purposes in the image below; they implement a line comment and flagging an 's' as special when it's not escaped so that it's clearer when that is the case.



    The third rule is what matches a valid escape sequence, which is a / followed by either another / (literal /) or an s (literal s).



    The last rule, which is optional, matches a / character followed by anything that's not a valid escape character (including not being followed by anything) and marks it as invalid as a hint to the user that they've done something silly.



    Here I've added a space character as a potentially valid escape for the purposes of invalid escape detection so that a trailing space following a bare / doesn't get the invalid scope as well. Without that, the syntax assumes you were trying to quote the space character.



    %YAML 1.2
    ---
    scope: source.example
    contexts:
    main:
    # A simple single-line comment scope capture for nice output
    - match: '(#).*n?'
    captures:
    0: comment.line.number-sign.example
    1: punctuation.definition.comment.example
    # Just so we can apply a color to the letter s to know when it's "special"
    - match: 's'
    scope: entity.name.special
    # The set of things that can be valid escapes
    - match: '/[s/]'
    scope: constant.character.escape.example
    # If only s and / are valid escapes, make other escapes invalid, including
    # using a bare / with nothing following it.
    - match: '/[^s/ ]?'
    scope: invalid.illegal.escape.example


    In a simple test, the results of this syntax would be:



    Syntax example






    share|improve this answer
























    • Thanks. I might go with this solution at a pinch... I can see now how I didn't explain myself fully, though (I'm effectively new to Stack Overflow, new to Sublime Text, New to YAML and relatively new to Regex, so I'm generally a bit flummoxed). I have revised my question to make explicit exactly what I am trying to achieve - I think that should make it clear - would you mind taking a look?

      – Dan Luba
      Nov 22 '18 at 4:35











    • For clarity, do you expect that when you enter some text in the file, the syntax highlight is also going to modify the text to remove some of the / characters, etc?

      – OdatNurd
      Nov 22 '18 at 6:48











    • No. No modification of text, just the highlighting as shown, to indicate what WILL BE removed by the expression interpreter of the display.

      – Dan Luba
      Nov 22 '18 at 7:31











    • You see how the grey slashes are easy to ignore and so make it much easier to visualize what the display will look like after the interpreter has done its work?

      – Dan Luba
      Nov 22 '18 at 7:53














    0












    0








    0







    Without more information on the structure of the syntax you currently have, it's hard to say for sure, but I think what you're looking for here is just a simple single match construct that matches the literal character / followed by any character that is allowed to be an escape character, which in your example is the character s and the character /.



    You may be getting hung up on thinking that the regular expression that you're trying to create needs to find the biggest possible match containing any combination of escape sequences. However, the match rules that you provide are used as many times as needed to consume the input, so they only need to match single syntax constructs at a time.



    A simple example of this in action is the following minimal syntax. The first two match constructs are just for example purposes in the image below; they implement a line comment and flagging an 's' as special when it's not escaped so that it's clearer when that is the case.



    The third rule is what matches a valid escape sequence, which is a / followed by either another / (literal /) or an s (literal s).



    The last rule, which is optional, matches a / character followed by anything that's not a valid escape character (including not being followed by anything) and marks it as invalid as a hint to the user that they've done something silly.



    Here I've added a space character as a potentially valid escape for the purposes of invalid escape detection so that a trailing space following a bare / doesn't get the invalid scope as well. Without that, the syntax assumes you were trying to quote the space character.



    %YAML 1.2
    ---
    scope: source.example
    contexts:
    main:
    # A simple single-line comment scope capture for nice output
    - match: '(#).*n?'
    captures:
    0: comment.line.number-sign.example
    1: punctuation.definition.comment.example
    # Just so we can apply a color to the letter s to know when it's "special"
    - match: 's'
    scope: entity.name.special
    # The set of things that can be valid escapes
    - match: '/[s/]'
    scope: constant.character.escape.example
    # If only s and / are valid escapes, make other escapes invalid, including
    # using a bare / with nothing following it.
    - match: '/[^s/ ]?'
    scope: invalid.illegal.escape.example


    In a simple test, the results of this syntax would be:



    Syntax example






    share|improve this answer













    Without more information on the structure of the syntax you currently have, it's hard to say for sure, but I think what you're looking for here is just a simple single match construct that matches the literal character / followed by any character that is allowed to be an escape character, which in your example is the character s and the character /.



    You may be getting hung up on thinking that the regular expression that you're trying to create needs to find the biggest possible match containing any combination of escape sequences. However, the match rules that you provide are used as many times as needed to consume the input, so they only need to match single syntax constructs at a time.



    A simple example of this in action is the following minimal syntax. The first two match constructs are just for example purposes in the image below; they implement a line comment and flagging an 's' as special when it's not escaped so that it's clearer when that is the case.



    The third rule is what matches a valid escape sequence, which is a / followed by either another / (literal /) or an s (literal s).



    The last rule, which is optional, matches a / character followed by anything that's not a valid escape character (including not being followed by anything) and marks it as invalid as a hint to the user that they've done something silly.



    Here I've added a space character as a potentially valid escape for the purposes of invalid escape detection so that a trailing space following a bare / doesn't get the invalid scope as well. Without that, the syntax assumes you were trying to quote the space character.



    %YAML 1.2
    ---
    scope: source.example
    contexts:
    main:
    # A simple single-line comment scope capture for nice output
    - match: '(#).*n?'
    captures:
    0: comment.line.number-sign.example
    1: punctuation.definition.comment.example
    # Just so we can apply a color to the letter s to know when it's "special"
    - match: 's'
    scope: entity.name.special
    # The set of things that can be valid escapes
    - match: '/[s/]'
    scope: constant.character.escape.example
    # If only s and / are valid escapes, make other escapes invalid, including
    # using a bare / with nothing following it.
    - match: '/[^s/ ]?'
    scope: invalid.illegal.escape.example


    In a simple test, the results of this syntax would be:



    Syntax example







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 22 '18 at 0:00









    OdatNurdOdatNurd

    9,50622638




    9,50622638













    • Thanks. I might go with this solution at a pinch... I can see now how I didn't explain myself fully, though (I'm effectively new to Stack Overflow, new to Sublime Text, New to YAML and relatively new to Regex, so I'm generally a bit flummoxed). I have revised my question to make explicit exactly what I am trying to achieve - I think that should make it clear - would you mind taking a look?

      – Dan Luba
      Nov 22 '18 at 4:35











    • For clarity, do you expect that when you enter some text in the file, the syntax highlight is also going to modify the text to remove some of the / characters, etc?

      – OdatNurd
      Nov 22 '18 at 6:48











    • No. No modification of text, just the highlighting as shown, to indicate what WILL BE removed by the expression interpreter of the display.

      – Dan Luba
      Nov 22 '18 at 7:31











    • You see how the grey slashes are easy to ignore and so make it much easier to visualize what the display will look like after the interpreter has done its work?

      – Dan Luba
      Nov 22 '18 at 7:53



















    • Thanks. I might go with this solution at a pinch... I can see now how I didn't explain myself fully, though (I'm effectively new to Stack Overflow, new to Sublime Text, New to YAML and relatively new to Regex, so I'm generally a bit flummoxed). I have revised my question to make explicit exactly what I am trying to achieve - I think that should make it clear - would you mind taking a look?

      – Dan Luba
      Nov 22 '18 at 4:35











    • For clarity, do you expect that when you enter some text in the file, the syntax highlight is also going to modify the text to remove some of the / characters, etc?

      – OdatNurd
      Nov 22 '18 at 6:48











    • No. No modification of text, just the highlighting as shown, to indicate what WILL BE removed by the expression interpreter of the display.

      – Dan Luba
      Nov 22 '18 at 7:31











    • You see how the grey slashes are easy to ignore and so make it much easier to visualize what the display will look like after the interpreter has done its work?

      – Dan Luba
      Nov 22 '18 at 7:53

















    Thanks. I might go with this solution at a pinch... I can see now how I didn't explain myself fully, though (I'm effectively new to Stack Overflow, new to Sublime Text, New to YAML and relatively new to Regex, so I'm generally a bit flummoxed). I have revised my question to make explicit exactly what I am trying to achieve - I think that should make it clear - would you mind taking a look?

    – Dan Luba
    Nov 22 '18 at 4:35





    Thanks. I might go with this solution at a pinch... I can see now how I didn't explain myself fully, though (I'm effectively new to Stack Overflow, new to Sublime Text, New to YAML and relatively new to Regex, so I'm generally a bit flummoxed). I have revised my question to make explicit exactly what I am trying to achieve - I think that should make it clear - would you mind taking a look?

    – Dan Luba
    Nov 22 '18 at 4:35













    For clarity, do you expect that when you enter some text in the file, the syntax highlight is also going to modify the text to remove some of the / characters, etc?

    – OdatNurd
    Nov 22 '18 at 6:48





    For clarity, do you expect that when you enter some text in the file, the syntax highlight is also going to modify the text to remove some of the / characters, etc?

    – OdatNurd
    Nov 22 '18 at 6:48













    No. No modification of text, just the highlighting as shown, to indicate what WILL BE removed by the expression interpreter of the display.

    – Dan Luba
    Nov 22 '18 at 7:31





    No. No modification of text, just the highlighting as shown, to indicate what WILL BE removed by the expression interpreter of the display.

    – Dan Luba
    Nov 22 '18 at 7:31













    You see how the grey slashes are easy to ignore and so make it much easier to visualize what the display will look like after the interpreter has done its work?

    – Dan Luba
    Nov 22 '18 at 7:53





    You see how the grey slashes are easy to ignore and so make it much easier to visualize what the display will look like after the interpreter has done its work?

    – Dan Luba
    Nov 22 '18 at 7:53











    0














    Alright, so for what it's worth, I figured it out. This:



    %YAML 1.2
    ---

    file_extensions:
    - example

    scope: source.example

    contexts:
    main:
    # special color setup #

    - match: s
    scope: green


    # desired behaviour #

    - match: '(/)(.)?'
    captures:
    1: grey
    2: white


    Gives you this:



    Appropriate representation of escape character behaviour






    share|improve this answer




























      0














      Alright, so for what it's worth, I figured it out. This:



      %YAML 1.2
      ---

      file_extensions:
      - example

      scope: source.example

      contexts:
      main:
      # special color setup #

      - match: s
      scope: green


      # desired behaviour #

      - match: '(/)(.)?'
      captures:
      1: grey
      2: white


      Gives you this:



      Appropriate representation of escape character behaviour






      share|improve this answer


























        0












        0








        0







        Alright, so for what it's worth, I figured it out. This:



        %YAML 1.2
        ---

        file_extensions:
        - example

        scope: source.example

        contexts:
        main:
        # special color setup #

        - match: s
        scope: green


        # desired behaviour #

        - match: '(/)(.)?'
        captures:
        1: grey
        2: white


        Gives you this:



        Appropriate representation of escape character behaviour






        share|improve this answer













        Alright, so for what it's worth, I figured it out. This:



        %YAML 1.2
        ---

        file_extensions:
        - example

        scope: source.example

        contexts:
        main:
        # special color setup #

        - match: s
        scope: green


        # desired behaviour #

        - match: '(/)(.)?'
        captures:
        1: grey
        2: white


        Gives you this:



        Appropriate representation of escape character behaviour







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 23 '18 at 23:14









        Dan LubaDan Luba

        175




        175






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53403301%2fregex-pattern-to-produce-appropriate-escape-character-behavior%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            Tangent Lines Diagram Along Smooth Curve

            Yusuf al-Mu'taman ibn Hud

            Zucchini