A
A
AleksVersus2021-04-10 00:15:46
Regular Expressions
AleksVersus, 2021-04-10 00:15:46

We need a regular expression with an alternative between the beginning of the string and a certain character. Is it possible to write like this?

I'm trying to write syntax highlighting for Sublime Text, and accordingly I work with the sublime-syntax file, where everything is written in regular expressions. I ran into this problem:

You need to get a string that contains the word act, while before the word act there can be either a brace character {, or the beginning of the line, followed by several spaces. It occurred to me to write like this:

(^|\{)(\s*?)(act)

but, firstly, it does not work, and secondly, I feel in my gut that this is Petrovna's nonsense. You can, of course, remove the second group and put to ^ and \{ respectively, but you need to refer to this group later. Is it possible to solve such a problem?

Sublime Text works with Boost Regex as far as I remember.

Syntax example:

count=count+23
*pl count
act "Действие":
    *pl 2345
end
{act "Действие 2":
     count=234+567
end}


Context example (does not work for curly braces):
- match: '(^|\{)(\s*?)(act)(?=\s*?.*?\:\s*?$)'
      captures:
        3: support.function.qsp avs.actionsopen1
      push:
        - match: ^(\2)(\s*?)(end)([^\&\}]*?)
          captures:
            2: invalid.illegal.expected-mapping-key.qsp avs.error avs.leftspace
            3: support.function.qsp avs.actionsend
            4: constant.numeric avs.numberic
          pop: true
        - include: locations

As mentioned in the comment below, the regular expression works. Apparently I'm stupid, sorry. I remove the question, because it does not work when searching for act in curly braces. But this seems to stem from my misunderstanding of Sublime Text's stack parsing. To make it work, I just wrote two different cases: for the beginning of a line, and with a look-behind for a curly brace.
- match: '(?<=\{)(\s*?)(act)(?=\s*?.*?\:\s*?$)'
      captures:
        2: support.function.qsp avs.actionsopen1
      push:
        - match: ^(\1)(\s*?)(end)([^\&\}]*?)
          captures:
            2: invalid.illegal.expected-mapping-key.qsp avs.error avs.leftspace
            3: support.function.qsp avs.actionsend
            4: constant.numeric avs.numberic
          pop: true
        - include: locations
    - match: '^(\s*?)(act)(?=\s*?.*?\:\s*?$)'
      captures:
        2: support.function.qsp avs.actionsopen1
      push:
        - match: ^(\1)(\s*?)(end)([^\&]*?)
          captures:
            2: invalid.illegal.expected-mapping-key.qsp avs.error avs.leftspace
            3: support.function.qsp avs.actionsend
            4: constant.numeric avs.numberic
          pop: true
        - include: locations

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
AleksVersus, 2021-04-10
@AleksVersus

As vreitech rightly pointed out, the regex works.
The error stems from my misunderstanding of how Sublime Text parses the text inside the stack (I already asked a question about this here on Habré, sorry). It does not treat this text directly as a separate line, hence the effect of curly braces on code highlighting within the braces themselves. In the header I wrote how I solved the problem. It's not elegant at all and looks like a crutch, but I can't think of another solution.

S
Stalker_RED, 2021-04-10
@Stalker_RED

(?:^|{)\s*?(act)
The curly bracket does not need to be escaped (although xs, maybe in sublime some dialect for which you need it)
https://regex101.com/r/iaAEvV/2
You would outline some example of the syntax with which you work. why are you asking what doesn't work now?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question