The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages - More
> Regex Programming
|
Other - Test Conditional
Discuss Test Conditional in the Regex Programming forum on Dev Shed. Test Conditional Regular expressions forum covering PCRE and POSIX techniques, practices, and standards. Regular expressions help shorten coding time by providing the ability to compact many lines of code into one string.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

February 7th, 2012, 12:40 PM
|
 |
JavaScript is not spelt java
|
|
Join Date: Feb 2011
Location: Landan, England
|
|
|
Other - 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.
|

February 7th, 2012, 04:01 PM
|
 |
Turn left at the third duck
|
|
Join Date: Dec 2011
Location: Nelson, NZ
|
|
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.
|

February 7th, 2012, 05:02 PM
|
 |
JavaScript is not spelt java
|
|
Join Date: Feb 2011
Location: Landan, England
|
|
|
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..
|

February 7th, 2012, 05:40 PM
|
 |
Turn left at the third duck
|
|
Join Date: Dec 2011
Location: Nelson, NZ
|
|
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.
|

February 8th, 2012, 01:08 AM
|
|
|
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}
|

February 8th, 2012, 03:50 AM
|
 |
Turn left at the third duck
|
|
Join Date: Dec 2011
Location: Nelson, NZ
|
|
hi aba, thanks for explaining what this is all about. 
|

February 8th, 2012, 07:56 AM
|
 |
JavaScript is not spelt java
|
|
Join Date: Feb 2011
Location: Landan, England
|
|
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.
|

February 8th, 2012, 02:27 PM
|
 |
JavaScript is not spelt java
|
|
Join Date: Feb 2011
Location: Landan, England
|
|
|
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
|

February 8th, 2012, 07:05 PM
|
|
|
|
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/).
|

February 9th, 2012, 07:04 AM
|
 |
JavaScript is not spelt java
|
|
Join Date: Feb 2011
Location: Landan, England
|
|
Quote: | 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.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|