6

I concocted the following regular expression, that is supposed to give me all text in the view that comes before the second to last }:

region = currentView.find("(?<=\\})[^\\}]+\\}[^\\}]*$", 0)

Sublime Text does not seem to think that $ means "end of whatever is in the view." To be honest, I don't quite understand what Sublime Text thinks it means.

My regular expression seems to work, as shown here on regexr.com.

I found a workaround for my particular cirmunstance that I can live with for the moment:

regions = currentView.find_all("\}")
    if len(regions) > 1:
        # stuff I am doing with regions[-2] goes here

but I would like to know if it is possible to match against the end of the view's content.

4
  • $ in regex means "end of string". So is your end of view the end of the string ?
    – HamZa
    Oct 21, 2013 at 18:36
  • I don't know enough about how Sublime Text implements it's buffers. I guess what I am looking for is an end of buffer, and I assumed end of string would work...
    – DudeOnRock
    Oct 21, 2013 at 18:38
  • If you want to construct a regex, you need to know some input you're expecting. You then elaborate a pattern. So can you provide us some input ? Also I don't seem to understand why you're using \\} ? Isn't \} enough if you want to match a literal } ?
    – HamZa
    Oct 21, 2013 at 18:41
  • @HamZa: I provided an example in the link to regexr.com, for reasons of neatness and clarity, I didn't want to clutter up the body of the actual question, since my problem appears to be Sublime's API and not the actual regex. The regular expression above is in the form of a python string, which needs the / symbol to be escaped.
    – DudeOnRock
    Oct 21, 2013 at 18:44

1 Answer 1

12

$ at the end of a regex can mean either the end of line or the end of the input, depending on the engine and modifier flags that are passed in. A quick look at Sublime's doc show that it supports the \z boundary, which unequivocally means "the end of the input buffer". So try replacing your final $ with \z, to see if that helps.

3
  • Brilliant! It does exactly what I want. May I ask where in Sublime's docs you found the answer? I did look there myself quite extensibly!
    – DudeOnRock
    Oct 21, 2013 at 18:51
  • 4
    Here in Sublime's doc The very last sentence refer to the Boost syntax. Then, in Boost's documentation, we have: Buffer Bundaries
    – James
    Oct 21, 2013 at 18:54
  • Note that the address for the corresponding Sublime's documentation has changed: see Sublime Text Community Documentation.
    – James
    Feb 8, 2021 at 16:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.