Regex Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming Languages - MoreRegex Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old February 7th, 2012, 12:40 PM
AndrewSW's Avatar
AndrewSW AndrewSW is offline
JavaScript is not spelt java
Dev Shed Novice (500 - 999 posts)
 
Join Date: Feb 2011
Location: Landan, England
Posts: 743 AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level)AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level)AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level)AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level)AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level)AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level)AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level)AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 2 Weeks 1 Day 23 h 1 m 13 sec
Reputation Power: 164
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.

Reply With Quote
  #2  
Old February 7th, 2012, 04:01 PM
ragax's Avatar
ragax ragax is offline
Turn left at the third duck
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2011
Location: Nelson, NZ
Posts: 93 ragax User rank is Second Lieutenant (5000 - 10000 Reputation Level)ragax User rank is Second Lieutenant (5000 - 10000 Reputation Level)ragax User rank is Second Lieutenant (5000 - 10000 Reputation Level)ragax User rank is Second Lieutenant (5000 - 10000 Reputation Level)ragax User rank is Second Lieutenant (5000 - 10000 Reputation Level)ragax User rank is Second Lieutenant (5000 - 10000 Reputation Level)ragax User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Day 24 m 37 sec
Reputation Power: 92
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.
__________________
Regex Tutorial | Latest RegexBuddy Demo

Reply With Quote
  #3  
Old February 7th, 2012, 05:02 PM
AndrewSW's Avatar
AndrewSW AndrewSW is offline
JavaScript is not spelt java
Dev Shed Novice (500 - 999 posts)
 
Join Date: Feb 2011
Location: Landan, England
Posts: 743 AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level)AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level)AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level)AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level)AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level)AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level)AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level)AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 2 Weeks 1 Day 23 h 1 m 13 sec
Reputation Power: 164
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..

Reply With Quote
  #4  
Old February 7th, 2012, 05:40 PM
ragax's Avatar
ragax ragax is offline
Turn left at the third duck
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2011
Location: Nelson, NZ
Posts: 93 ragax User rank is Second Lieutenant (5000 - 10000 Reputation Level)ragax User rank is Second Lieutenant (5000 - 10000 Reputation Level)ragax User rank is Second Lieutenant (5000 - 10000 Reputation Level)ragax User rank is Second Lieutenant (5000 - 10000 Reputation Level)ragax User rank is Second Lieutenant (5000 - 10000 Reputation Level)ragax User rank is Second Lieutenant (5000 - 10000 Reputation Level)ragax User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Day 24 m 37 sec
Reputation Power: 92
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.

Reply With Quote
  #5  
Old February 8th, 2012, 01:08 AM
abareplace abareplace is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2011
Posts: 29 abareplace User rank is First Lieutenant (10000 - 20000 Reputation Level)abareplace User rank is First Lieutenant (10000 - 20000 Reputation Level)abareplace User rank is First Lieutenant (10000 - 20000 Reputation Level)abareplace User rank is First Lieutenant (10000 - 20000 Reputation Level)abareplace User rank is First Lieutenant (10000 - 20000 Reputation Level)abareplace User rank is First Lieutenant (10000 - 20000 Reputation Level)abareplace User rank is First Lieutenant (10000 - 20000 Reputation Level)abareplace User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 8 h 25 m 9 sec
Reputation 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}

Reply With Quote
  #6  
Old February 8th, 2012, 03:50 AM
ragax's Avatar
ragax ragax is offline
Turn left at the third duck
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2011
Location: Nelson, NZ
Posts: 93 ragax User rank is Second Lieutenant (5000 - 10000 Reputation Level)ragax User rank is Second Lieutenant (5000 - 10000 Reputation Level)ragax User rank is Second Lieutenant (5000 - 10000 Reputation Level)ragax User rank is Second Lieutenant (5000 - 10000 Reputation Level)ragax User rank is Second Lieutenant (5000 - 10000 Reputation Level)ragax User rank is Second Lieutenant (5000 - 10000 Reputation Level)ragax User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Day 24 m 37 sec
Reputation Power: 92
hi aba, thanks for explaining what this is all about.

Reply With Quote
  #7  
Old February 8th, 2012, 07:56 AM
AndrewSW's Avatar
AndrewSW AndrewSW is offline
JavaScript is not spelt java
Dev Shed Novice (500 - 999 posts)
 
Join Date: Feb 2011
Location: Landan, England
Posts: 743 AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level)AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level)AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level)AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level)AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level)AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level)AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level)AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 2 Weeks 1 Day 23 h 1 m 13 sec
Reputation Power: 164
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.

Reply With Quote
  #8  
Old February 8th, 2012, 02:27 PM
AndrewSW's Avatar
AndrewSW AndrewSW is offline
JavaScript is not spelt java
Dev Shed Novice (500 - 999 posts)
 
Join Date: Feb 2011
Location: Landan, England
Posts: 743 AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level)AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level)AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level)AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level)AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level)AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level)AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level)AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 2 Weeks 1 Day 23 h 1 m 13 sec
Reputation Power: 164
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

Reply With Quote
  #9  
Old February 8th, 2012, 07:05 PM
abareplace abareplace is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2011
Posts: 29 abareplace User rank is First Lieutenant (10000 - 20000 Reputation Level)abareplace User rank is First Lieutenant (10000 - 20000 Reputation Level)abareplace User rank is First Lieutenant (10000 - 20000 Reputation Level)abareplace User rank is First Lieutenant (10000 - 20000 Reputation Level)abareplace User rank is First Lieutenant (10000 - 20000 Reputation Level)abareplace User rank is First Lieutenant (10000 - 20000 Reputation Level)abareplace User rank is First Lieutenant (10000 - 20000 Reputation Level)abareplace User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 8 h 25 m 9 sec
Reputation 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/).

Reply With Quote
  #10  
Old February 9th, 2012, 07:04 AM
AndrewSW's Avatar
AndrewSW AndrewSW is offline
JavaScript is not spelt java
Dev Shed Novice (500 - 999 posts)
 
Join Date: Feb 2011
Location: Landan, England
Posts: 743 AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level)AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level)AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level)AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level)AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level)AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level)AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level)AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 2 Weeks 1 Day 23 h 1 m 13 sec
Reputation Power: 164
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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreRegex Programming > Other - Test Conditional

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap