My Regex skills are limited, can anyone assist with this one. I have the following which matches and replaces fine however I want to get only the bit between the square brackets themselves?
This is basically for replacing shortcodes for playing cards. Expected input would be :
Lorem ipsum dolor sit amet, [A9] consectetur adipiscing elit. Nulla faucibus semper viverra. Donec dignissim hendrerit egestas. Sed fermentum accumsan eros.
The bold above is the string to match and the A9 is the bit I am really interested in, I think possibly I need to look at name groups(?). I could cheat and use .net substrings but thought it may be possible as part of the Regex. Just bought a copy of RegexMagic + RegexBuddy to ease my pain with Regular Expressions unfortunately before I knew of Expresso :(
The RegEx you've supplied already creates two matchs/groups - they should be accessible somehow (on the RegEx object?) - e.g. in a JavaScript RegEx replace, you can do:
var re = /\[([1-9]|[AJKQ])([1-9]|[CDHS])\]/i
console.log("[A9]".replace(re, "$1$2")) // #=> A9
are you not able to select the (sub-)matches on that RegEx result?
I did Stefan, you are correct. In my haste I didn't check that I had put your Regex in before testing. Works a treat, thanks a bunch and marked as the solution!
We really could do with some additional forums on here to cover more generic programming stuff like this that is perhaps less umbraco specific.There is certainly the skills around to answer the questions if people have the time.
Regular Expression Help
Slightly off-topic but it is for a Razor macro :)
My Regex skills are limited, can anyone assist with this one. I have the following which matches and replaces fine however I want to get only the bit between the square brackets themselves?
It's often easier with these things if you give some examples of input (ie. "text") and your expected output for each value.
I use Expresso to write my RegEx (which isn't often), it's very useful! (written in .NET too) http://www.ultrapico.com/Expresso.htm
I had quite a headmess with my Shortcodes package, but the take a look at the source for the RegEx if it helps?
Cheers, Lee.
This is basically for replacing shortcodes for playing cards. Expected input would be :
The bold above is the string to match and the A9 is the bit I am really interested in, I think possibly I need to look at name groups(?). I could cheat and use .net substrings but thought it may be possible as part of the Regex. Just bought a copy of RegexMagic + RegexBuddy to ease my pain with Regular Expressions unfortunately before I knew of Expresso :(
http://regexr.com/ is a great tool for regex too, also has a desktop client :-)
Try this: \[((?:[1-9]|[AJKQ])(?:[1-9]|[CDHS]))\]
Simon,
Stefan's RegEx works when I test in Expresso - all good.
I got as far as this:
Cheers, Lee.
I get called "The RegEx guy" ;-)
@Lee, your Regex is, how should I call it, not strict enough ;-)
Hi Simon,
The RegEx you've supplied already creates two matchs/groups - they should be accessible somehow (on the RegEx object?) - e.g. in a JavaScript RegEx replace, you can do:
are you not able to select the (sub-)matches on that RegEx result?
/Chriztian
@Stefan: um, yes, I try to keep my distance from Regex patterns LOL ;-)
@Lee; well, I hated them at first too, until I started working with UrlRewriting. Now I LOVE RegEx :P
Thanks guys, great help! With Stefan's RegEx and Chriztian's pointer to the groups I have achived it. Solution as follows:
I thought you wanted 'A9' to be a single hit/group. My bad.
With my RegEx you should be able to do something like this (untested):
I did Stefan, you are correct. In my haste I didn't check that I had put your Regex in before testing. Works a treat, thanks a bunch and marked as the solution!
We really could do with some additional forums on here to cover more generic programming stuff like this that is perhaps less umbraco specific.There is certainly the skills around to answer the questions if people have the time.
Awesome :-)
is working on a reply...