Home Download Buy Blog Forum Support

regexes and contexts

regexes and contexts

Postby pierre on Wed Mar 23, 2011 1:53 pm

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
pierre
 
Posts: 28
Joined: Sat Mar 19, 2011 8:47 pm

Re: regexes and contexts

Postby guillermooo on Wed Mar 23, 2011 2:15 pm

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


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.

pierre wrote: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?


Try "regex_match" instead of "regex_contains" and see if that works.
guillermooo
 
Posts: 718
Joined: Thu Jul 23, 2009 9:06 am

Re: regexes and contexts

Postby pierre on Wed Mar 23, 2011 2:46 pm

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
pierre
 
Posts: 28
Joined: Sat Mar 19, 2011 8:47 pm

Re: regexes and contexts

Postby guillermooo on Wed Mar 23, 2011 3:01 pm

Can you show the whole key binding?
guillermooo
 
Posts: 718
Joined: Thu Jul 23, 2009 9:06 am

Re: regexes and contexts

Postby pierre on Wed Mar 23, 2011 5:42 pm

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
pierre
 
Posts: 28
Joined: Sat Mar 19, 2011 8:47 pm

Re: regexes and contexts

Postby pierre on Wed Mar 23, 2011 5:52 pm

PS well that one was too lame, my besr attempt is actually
"[^|^.*\\s$]"
pierre
 
Posts: 28
Joined: Sat Mar 19, 2011 8:47 pm

Re: regexes and contexts

Postby guillermooo on Wed Mar 23, 2011 8:01 pm

The following seems to work for me:
Code: Select all
   {
      "keys": ["ctrl+f8"],
      "command": "echo",
      "args": {
         "hello": "hello"
      },
      "context":
      [
         { "key": "preceding_text", "operator": "regex_match", "operand": "^$|.*?\\s$" }
      ]
   }
guillermooo
 
Posts: 718
Joined: Thu Jul 23, 2009 9:06 am

Re: regexes and contexts

Postby pierre on Wed Mar 23, 2011 9:54 pm

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
pierre
 
Posts: 28
Joined: Sat Mar 19, 2011 8:47 pm

Re: regexes and contexts

Postby guillermooo on Wed Mar 23, 2011 10:40 pm

Have you tried with another key sequence, like f1-f8, instead of ctrl+§? Does that work?
guillermooo
 
Posts: 718
Joined: Thu Jul 23, 2009 9:06 am

Re: regexes and contexts

Postby pierre on Wed Mar 23, 2011 11:25 pm

Correction: it does work. I was expecting the command to
output inside the main window :) 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
pierre
 
Posts: 28
Joined: Sat Mar 19, 2011 8:47 pm

Next

Return to Technical Support

Who is online

Users browsing this forum: Yahoo [Bot] and 18 guests