#1
  1. JavaScript is not spelt java
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2011
    Location
    Landan, England
    Posts
    743
    Rep Power
    165

    Test Conditional


    Hello. I've built the following expression, used within a completion/snippet of the Sublime Text 2 editor:

    Code:
    ${1:Absolute Relative Fixed Static}${1/\\w.+|[^arfs].*|(a)|(r)|(f)|(s)/?1:bsolute:?2:elative:?3:ixed:?4:tatic/}
    It basically says, if they type the letter 'a' add the text 'bsolute' after it to create the word 'absolute'.

    What I would like to do is capture (a|r|f|s) as one item, and test it to see which letter was caught. Something perhaps like:

    Code:
    /?1=a:bsolute:?1=r:elative etc. /
    Is this possible please? Andy.
  2. #2
  3. Turn left at the third duck
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2011
    Location
    Nelson, NZ
    Posts
    112
    Rep Power
    93
    Hi Andrew,

    A few considerations as a starting point to the convo, as I'm not entirely sure what will be the most useful to you. Just let me know which of these ideas (if any) goes in the right direction, and we can take it from there.

    1. In PCRE you can reset a regex capture group, so that two sets of parentheses go to the same group number:
    Code:
    (?|(a)bsolute|(r)elative)
    Both a and r (if captured) will be in Group 1.
    (Not supported in RegexBuddy, but will work in PHP and other environments using PCRE libraries)

    2. Here's one (contrived) example of regex conditionals:
    Code:
    ^(?:(a)|(r))(?(1)bsolute)?(?(2)elative)
    The "a" gets captured in Group 1, the "r" gets captured in Group 2.

    3. For me this is not a great use of conditionals: you can accomplish the same with
    Code:
    (?:(a)bsolute|(r)elative)
    where again "a" gets captured in Group 1 and the "r" gets captured in Group 2.

    Again, these are just ideas to get the ball rolling.
    Wishing you a fun day.
  4. #3
  5. JavaScript is not spelt java
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2011
    Location
    Landan, England
    Posts
    743
    Rep Power
    165
    Hello ragax, and thank you very much.

    This gives me something to go on, although at the moment I can't see how I can apply it to my expression.

    ${1:Absolute Relative Fixed Static}${1/\\w.+|[^arfs].*|(a)|(r)|(f)|(s)
    /?1:bsolute:?2:elative:?3:ixed:?4:tatic/}

    The first part is perhaps misleading, as the user is overtyping it with a letter a, r, f or s. I'm then appending 'bsolute', etc., after that letter.

    Any further push in the right direction would be welcome..
  6. #4
  7. Turn left at the third duck
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2011
    Location
    Nelson, NZ
    Posts
    112
    Rep Power
    93
    Hi Andrew,

    I am not familiar with the programming language you are using, so I don't understand the syntax you wrote. (Out of curiosity, what is the language?)

    It sounds like in the second set of curly braces, you want set a variable depending on a letter typed by the user. So you think your language will let you accomplish this in a single line of code, and will let you tap into the group captures directly, without giving them a variable name?

    In PHP you can tap into the captured data with \1, \2 etc within the regex itself, as in (a)\1, which would match "aa". Once you leave the regex, you can still access the captures, but through a variable. For instance, after you do this match: preg_match('(a)\1','aa',$m) you can access group 1 through $m[1]

    These are the only thoughts I can share. I am sure the solution will be obvious to someone who is competent in your language. Sorry I couldn't really help.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2011
    Posts
    29
    Rep Power
    0
    Ragax, the syntax is described at http://sublimetext.info/docs/en/extensibility/snippets.html

    Andrew, it's impressive that you solved your task (auto-completing the four words) with this limited syntax. As far as I understand, you cannot test which letter was caught. Please consult Sublime Text manual; the substitution syntax is specific for the editor and not related to regular expressions.

    CSS is case-insensitive, so you may wish to add /i option at the end. I also rewrote the first alternative (which catches the initial text "Absolute Relative Fixed Static"):

    Code:
    ${1:Absolute Relative Fixed Static}${1/Absolute Relative Fixed Static|[^arfs].*|(a)|(r)|(f)|(s)/?1:bsolute:?2:elative:?3:ixed:?4:tatic/i}
  10. #6
  11. Turn left at the third duck
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2011
    Location
    Nelson, NZ
    Posts
    112
    Rep Power
    93
    hi aba, thanks for explaining what this is all about.
  12. #7
  13. JavaScript is not spelt java
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2011
    Location
    Landan, England
    Posts
    743
    Rep Power
    165
    Thank you both. I've also got it to fill in characters to the left and right of where they're typing, which is kinda cool:

    Code:
    { "trigger": "backgroundRepeat", "contents": "background-repeat: ${1/([xy]$)|.*/?1:repeat\\-/}${1:No-repeat Repeat repeat-X repeat-Y}${1/(r$)|(n$)|.*/(?1:epeat:?2:o-repeat/i};$0" },
    But I'm still not at a resolution yet. I want to capture one group (a|r|s|f)$ as group 1, and to be able to check which letter was actually caught. Something like

    ?1=a:bsolute:?2=r:elative

    Is this possible?
    Edited: Oops, abareplace - I've just re-read your previous post (I was on a train..). Thank you - seems I'm reaching the limits of what can be achieved here. Andy.
    Last edited by AndrewSW; February 8th, 2012 at 08:35 AM.
  14. #8
  15. JavaScript is not spelt java
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2011
    Location
    Landan, England
    Posts
    743
    Rep Power
    165
    Continuing this topic, if I capture two groups $1 and $2, how do I express 'if 1 is this AND 2 is that?'.

    My example is, I want to check for the word 'inline' in 1 and the letter 'b' in 2, to then append 'lock' to the 'b'?

    Andy
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2011
    Posts
    29
    Rep Power
    0
    Andy, regular expressions don't define the substitution syntax. It's specific for the editor that you use and not related to regular expressions. You should try to find it in the manual or ask your question at Sublime Text forum (http://www.sublimetext.com/forum/).
  18. #10
  19. JavaScript is not spelt java
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2011
    Location
    Landan, England
    Posts
    743
    Rep Power
    165
    Originally Posted by abareplace
    Andy, regular expressions don't define the substitution syntax. It's specific for the editor that you use and not related to regular expressions. You should try to find it in the manual or ask your question at Sublime Text forum (http://www.sublimetext.com/forum/).
    Okay, and thanks for coming back to me. Andy.

IMN logo majestic logo threadwatch logo seochat tools logo