Home Download Buy Blog Forum Support

Regex keybind, how to match literal asterisk?

Regex keybind, how to match literal asterisk?

Postby Anomareh on Fri Apr 08, 2011 2:30 pm

Code: Select all
{
    "keys": ["enter"], "command": "insert_snippet",
    "args":
        {
            "contents": "\n*$0b"
        },
    "context":
    [
        { "key": "preceding_text", "operator": "regex_contains", "operand": "\\*", "match_all": true }
    ]
}

Why does that keybind match:

Code: Select all
* Whatever|

with the pipe being the cursor position.

I'm guessing because it's translating that to: "\\*" (e.g. 0 or more backslashes)? If so, how do you match a literal asterisk? Using one backslash causes a parse error and a single or double asterisk doesn't work.
Anomareh
 
Posts: 222
Joined: Thu Oct 01, 2009 7:32 pm

Re: Regex keybind, how to match literal asterisk?

Postby adzenith on Fri Apr 08, 2011 4:37 pm

What if you changed the "\\*" to "\\*$"? I'm not sure how "preceding_text" works.
To be 100% sure you're capturing an asterisk character, you could use "[*]".
adzenith
 
Posts: 1217
Joined: Mon Oct 19, 2009 9:12 pm

Re: Regex keybind, how to match literal asterisk?

Postby Anomareh on Fri Apr 08, 2011 5:22 pm

Bah, yeah you're right. For whatever reason I thought preceding_text would only match if it was directly preceding the cursor. So "\\*" would only match "*|" and not if there was just an asterisk anywhere behind the cursor. Thanks.
Anomareh
 
Posts: 222
Joined: Thu Oct 01, 2009 7:32 pm

Re: Regex keybind, how to match literal asterisk?

Postby jps on Fri Apr 08, 2011 10:28 pm

As adzenith suggested, "[*]$" is what you want: a single asterisk just before the cursor. Escaping is a little tricky, as you have to escape the JSON encoding and the regex encoding, which generally means just doubling the number of backslashes you think you need, so another option is "\\\\*$"
jps
Site Admin
 
Posts: 3071
Joined: Wed Mar 19, 2008 12:33 pm

Re: Regex keybind, how to match literal asterisk?

Postby Anomareh on Fri Apr 08, 2011 10:44 pm

Aye, my issue was more the dollar sign than it was the asterisk. I just didn't realize that it was matching against everything before and after the cursor. I thought if the match wasn't attached to the cursor it wouldn't match. Also "\\*" seems to match an asterisk, don't think 4 is needed. Thanks again.
Anomareh
 
Posts: 222
Joined: Thu Oct 01, 2009 7:32 pm


Return to Technical Support

Who is online

Users browsing this forum: Google [Bot], Yahoo [Bot] and 25 guests