Sublime Forum

[Solved] How can I specify regular expression flags?

#1

I’ve always thought ST3 was missing the ability to set custom modification flags to alter the behaviour of the provided expression pattern, mostly just because we (the users) only provide the pattern, without the wrapping “/” characters.

This afternoon I stumbled onto a SO answer where the user “MRAB” says these may be set inline in the query… For example, setting the “s” flag to make dot match new lines as well. I then tried setting a few different flags with some simple patterns, but the common flags like “g” or “m” seem to cause errors that prevent any matches.

After this I went to look through both the official and unofficial documentation, but I can’t find anything about this feature in the docs. It’s possible that I’m simply searching the docs for the wrong keywords, so could somebody point me to some more information on this? Or, if it really doesn’t exist, perhaps tell me I’m not going mad (yet)?


As an alternative to my question above: is there any way to get ST3’s search bar to give us access to the full “/(...)/gmixXsuUAJD” expression (pattern and example flags) when the “Regular Expression” search option is enabled?

0 Likes

#2

from what I can tell, the default flags ST uses are:

  • g: match more than once (don’t stop at first match)
  • m: ^ and $ match the beginning and end of lines, respectively
  • i: (depending on your Case Sensitive toggle state)

  • there is no way to disable g as far as I can tell, as it isn’t an inline option. If you don’t want to have multiple matches (I guess for performance reasons), maybe it is possible to make a condition in the regex to assert that it hasn’t matched previously?
  • boost doesn’t support X, u, U, A, J or D inline flags it seems, but it already operates in full unicode mode and the other flags don’t seem to be very useful from what I can see
  • if you want to disable m, you can use (?-m)
  • you can override case (in)sensitivity with (?i) or (?-i)
  • if you want . to match new lines, you can use (?s)
  • to enable/disable extended mode (ignoring whitespace in the expression and affecting comments) you can use (?x) and (?-x)

all these work perfectly in my testing

nope

2 Likes

#3

@kingkeith… Thank you, seriously. That clears up my main question perfectly. I must admit, this…

…makes perfect sense. Especially in my case, as this is just a search pattern within the text editor scenario; so I’ll simply step through the matches manually anyway.

However, it was this…

…that properly clarified it’s scope for me. I completely forgot to check which engine was used by ST3 to execute regular expressions before I posted my question… Apologies. :blush:

That said, thanks for the useful crash course on boost syntax, really appreciate that. :beers:


[edit] is there any way (or need) to mark a thread as “solved” or “closed”?

1 Like

#4

for reference, a link to the unofficial docs where it states that the Boost regex engine is used:
http://docs.sublimetext.info/en/latest/search_and_replace/search_and_replace_overview.html?highlight=boost

I believe that it is a useful thing to do - so that if someone searches for something similar in future, they can clearly see which results might be most helpful. The most common practice I’ve seen is to put [Solved] at the beginning of the thread title.

0 Likes

#5

Awesome, all sorted. :thumbsup:

0 Likes