Sublime Forum

Regexes and contexts

#1

I have some troubles in finding the correct syntax to use
when defining a context via regular expressions. I’m talking
of constructs of the form
{“key” : “preceding_text”, “operator” : “regex_contains”, “operand” : “.*\s$”, “match_all”: true}

I want to check if preceding_test ends with a space or not.
But $ is not the correct wildchar to use to mark the end of
preceding_text. What should I use? Where can I find
details/rich examples of this kind of constructs?

Also, \s (for space) gives an error and I must use \s
but \n or \t do not give errors. Why?

Thank you all for your patience
Piero

0 Likes

#2

[quote=“pierre”]Also, \s (for space) gives an error and I must use \s
but \n or \t do not give errors. Why?[/quote]

You’re defining regexes in JSON, so you need to account for JSON’s own escaping sequences. “” is the escape character in JSON, so “\” gives you a single “” after Sublime parses the JSON. Which means, “\s” in JSON equals “\s” after parsing–exactly what you want. “\t” and “\n” happen to be valid JSON escape sequences.

[quote=“pierre”]I want to check if preceding_test ends with a space or not.
But $ is not the correct wildchar to use to mark the end of
preceding_text. What should I use? Where can I find
details/rich examples of this kind of constructs?[/quote]

Try “regex_match” instead of “regex_contains” and see if that works.

0 Likes

#3

Thank you for the explanation about JSON, sorry if the
question was too stupid.

As to regex_match: I tried that as well, but it does not
work

0 Likes

#4

Can you show the whole key binding?

0 Likes

#5

Sure, this is the full content of my Default keybinding file:

{ "keys": "`","a"], "command": "insert_snippet", "args": {"contents": "\\alpha"}, "context": 
	
		{"key" : "selector", "operator" : "equal", "operand" : "text.tex.latex", "match_all": true},
		{"key" : "preceding_text", "operator" : "regex_contains", "operand" : ".*\\s", "match_all": true}
	]
}

]

This works fine but of course captures any space, while I want to capture
a space just at the end of preceding_text (i.e., I want to be sure that
there is a space at the left when I start to use my keybinding).

Actually, I would like to known the correct regex to capture
" either the cursor is at the beginning of line, or there is a space at the left of it"
but my lame attempts like “^|^.*$]” do not seem to work

Piero

0 Likes

#6

PS well that one was too lame, my besr attempt is actually
“^|^.*\s$]”

0 Likes

#7

The following seems to work for me:

[code] {
“keys”: “ctrl+f8”],
“command”: “echo”,
“args”: {
“hello”: “hello”
},
“context”:

		{ "key": "preceding_text", "operator": "regex_match", "operand": "^$|.*?\\s$" }
	]
}

[/code]

0 Likes

#8

Thank you for wasting time on this.

Ok so: my User/Default (OSX).sublime-keymap file contains only:

{
  "keys": "ctrl+§"],
  "command": "echo",
  "args": {
     "hello": "hello"
  },
  "context":
  
     { "key": "preceding_text", "operator": "regex_match", "operand": "^$|.*?\\s$" }
  ]

}
]

If I understand correctly, this means that a ctrl+§ should write “hello” if
the cursor is after a space or at the beginning of a line, otherwise
do nothing.
On my macbook, it does nothing at all, ever. I tried many different
situations. Might this possibly be a mac thing?

Piero

0 Likes

#9

Have you tried with another key sequence, like f1-f8, instead of ctrl+§? Does that work?

0 Likes

#10

Correction: it does work. I was expecting the command to
output inside the main window :smile: instead of the console.

Thanks a lot. If more examples were available I would
have found out without annoying the list. Though, it still
is not clear to me why .* does not work and .*? works
instead… Anyway, $ is the correct reference to the end
of the previous text

Piero

0 Likes

#11

Well, actually, to be precise.

This works correctly: when I press ‘a’ at beginning of line or
after a space, \alpha is inserted:
{
“keys”: “a”],
“command”: “insert_snippet”,
“args”: {“contents”: “\alpha”},
“context”:

    { "key": "preceding_text", "operator": "regex_contains", "operand": "^$|.*?\\s$" }
  ]

}

But this does not work: if I press ‘a’ and then ‘b’ nothing happens:
{
“keys”: “a”, “b”],
“command”: “insert_snippet”,
“args”: {“contents”: “\alpha”},
“context”:

    { "key": "preceding_text", "operator": "regex_contains", "operand": "^$|.*?\\s$" }
  ]

}

So the problem is with multiple key bindings. If I remove
the $ character from the regex, the binding works, but
it captures any space and not only a space before the
cursor. Is this the expected behaviour?

0 Likes