Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Simon Dingley 1474 posts 3431 karma points c-trib
    Jan 27, 2012 @ 13:43
    Simon Dingley
    0

    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?

            private string ParseHands(string text)
            {
                return Regex.Replace(text, @"\[([1-9]|[AJKQ])([1-9]|[CDHS])\]",
                    card => string.Format("{0}.png", card), RegexOptions.IgnoreCase);
            }
  • Dan Diplo 1554 posts 6205 karma points MVP 6x c-trib
    Jan 27, 2012 @ 14:34
    Dan Diplo
    0

    It's often easier with these things if you give some examples of input (ie. "text") and your expected output for each value. 

  • Lee Kelleher 4024 posts 15833 karma points MVP 13x admin c-trib
    Jan 27, 2012 @ 14:38
    Lee Kelleher
    0

    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.

  • Simon Dingley 1474 posts 3431 karma points c-trib
    Jan 27, 2012 @ 14:43
    Simon Dingley
    0

    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 :(

  • Stefan Kip 1614 posts 4131 karma points c-trib
    Jan 27, 2012 @ 14:49
    Stefan Kip
    2

    http://regexr.com/ is a great tool for regex too, also has a desktop client :-)

    Try this: \[((?:[1-9]|[AJKQ])(?:[1-9]|[CDHS]))\]

     

  • Lee Kelleher 4024 posts 15833 karma points MVP 13x admin c-trib
    Jan 27, 2012 @ 14:51
    Lee Kelleher
    0

    Simon,

    Stefan's RegEx works when I test in Expresso - all good.

    I got as far as this:

    \[([1-9AJKQCDHS]*)\]

    Cheers, Lee.

  • Stefan Kip 1614 posts 4131 karma points c-trib
    Jan 27, 2012 @ 14:52
    Stefan Kip
    0

    I get called "The RegEx guy" ;-)

    @Lee, your Regex is, how should I call it, not strict enough ;-)

  • Chriztian Steinmeier 2798 posts 8788 karma points MVP 8x admin c-trib
    Jan 27, 2012 @ 14:54
    Chriztian Steinmeier
    0

    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:

    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? 

    /Chriztian

  • Lee Kelleher 4024 posts 15833 karma points MVP 13x admin c-trib
    Jan 27, 2012 @ 15:00
    Lee Kelleher
    0

    @Stefan: um, yes, I try to keep my distance from Regex patterns LOL ;-)

  • Stefan Kip 1614 posts 4131 karma points c-trib
    Jan 27, 2012 @ 15:01
    Stefan Kip
    0

    @Lee; well, I hated them at first too, until I started working with UrlRewriting. Now I LOVE RegEx :P

  • Simon Dingley 1474 posts 3431 karma points c-trib
    Jan 27, 2012 @ 15:23
    Simon Dingley
    0

    Thanks guys, great help! With Stefan's RegEx and Chriztian's pointer to the groups I have achived it. Solution as follows:

            private string ParseHands(string text)
            {
                return Regex.Replace(text, @"\[([1-9]|[AJKQ])([1-9]|[CDHS])\]",
                    card => string.Format("{0}{1}.png", card.Groups[1].Value, card.Groups[2].Value), RegexOptions.IgnoreCase);
            }

     

  • Stefan Kip 1614 posts 4131 karma points c-trib
    Jan 27, 2012 @ 15:24
    Stefan Kip
    0

    I thought you wanted 'A9' to be a single hit/group. My bad.

  • Stefan Kip 1614 posts 4131 karma points c-trib
    Jan 27, 2012 @ 15:26
    Stefan Kip
    1

    With my RegEx you should be able to do something like this (untested):

    private string ParseHands(string text)
    {
        return Regex.Replace(text, @"\[((?:[1-9]|[AJKQ])(?:[1-9]|[CDHS]))\]",
            card => string.Concat(card.Groups[1].Value, ".png"), RegexOptions.IgnoreCase);
    }
  • Simon Dingley 1474 posts 3431 karma points c-trib
    Jan 27, 2012 @ 15:30
    Simon Dingley
    0

    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.

  • Stefan Kip 1614 posts 4131 karma points c-trib
    Jan 27, 2012 @ 15:33
    Stefan Kip
    0

    Awesome :-)

Please Sign in or register to post replies

Write your reply to:

Draft