a helpful idea if you're looking for something to do

Discussion in 'Ruby' started by Simon Schuster, Aug 16, 2007.

  1. compile a whole lot of ruby regex examples, with commentary on what's
    going on. the few websites I've found, and books I've looked through
    just touch on the basics with minimal examples and explanation, or are
    specifically for perl/etc. a nice-looking and lengthy site could be
    extremely helpful to a lot of people starting with ruby, I imagine.

    - dealing with unicode?
    - mingling literal " / \ etc, with their regex counterparts, in ways
    that would be daunting for the inexperienced
    - just generally "higher-level" regex, leave the "intro to regex" to
    all the other places. that's easy enough to find.
     
    Simon Schuster, Aug 16, 2007
    #1
    1. Advertising

  2. Simon Schuster

    Konrad Meyer Guest

    --DSPAM_MULTIPART_EX-12044
    Content-Type: multipart/signed;
    boundary="nextPart1309867.UVUxRD5DY7";
    protocol="application/pgp-signature";
    micalg=pgp-sha1

    --nextPart1309867.UVUxRD5DY7
    Content-Type: text/plain;
    charset="utf-8"
    Content-Transfer-Encoding: quoted-printable
    Content-Disposition: inline

    On Thursday 16 August 2007 09:36:13 am Simon Schuster wrote:
    > compile a whole lot of ruby regex examples, with commentary on what's
    > going on. the few websites I've found, and books I've looked through
    > just touch on the basics with minimal examples and explanation, or are
    > specifically for perl/etc. a nice-looking and lengthy site could be
    > extremely helpful to a lot of people starting with ruby, I imagine.
    >=20
    > - dealing with unicode?
    > - mingling literal " / \ etc, with their regex counterparts, in ways
    > that would be daunting for the inexperienced
    > - just generally "higher-level" regex, leave the "intro to regex" to
    > all the other places. that's easy enough to find.


    =46or the most part, the ruby regex engine is perl-like. And instead of hav=
    ing
    to escape /s, we get stuff like %r@regex/bar@i. ZenSpider's Ruby QuickRef i=
    s a
    great place to go for beginner help.

    http://www.zenspider.com/Languages/Ruby/QuickRef.html#11

    =2D-=20
    Konrad Meyer <> http://konrad.sobertillnoon.com/

    --nextPart1309867.UVUxRD5DY7
    Content-Type: application/pgp-signature; name=signature.asc
    Content-Description: This is a digitally signed message part.

    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.4.7 (GNU/Linux)

    iD8DBQBGxI8pCHB0oCiR2cwRAjyuAJ9t1p0TMLSkCOvE8ZQYH6iI1G32TwCfc0pq
    cZHRMq7aryX+pjfrIdf5Fuc=
    =RyOV
    -----END PGP SIGNATURE-----

    --nextPart1309867.UVUxRD5DY7--

    --DSPAM_MULTIPART_EX-12044
    Content-Type: text/plain
    X-DSPAM-Signature: 46c48f2e120441198164824

    !DSPAM:46c48f2e120441198164824!
    --DSPAM_MULTIPART_EX-12044--
     
    Konrad Meyer, Aug 16, 2007
    #2
    1. Advertising

  3. Simon Schuster

    Dan Zwell Guest

    Simon Schuster wrote:
    > compile a whole lot of ruby regex examples, with commentary on what's
    > going on. the few websites I've found, and books I've looked through
    > just touch on the basics with minimal examples and explanation, or are
    > specifically for perl/etc. a nice-looking and lengthy site could be
    > extremely helpful to a lot of people starting with ruby, I imagine.
    >
    > - dealing with unicode?

    This one bothered me a lot, but the solution is simple. At the beginning
    of the document, set
    $KCODE = "u"

    This will fix regex behavior for use with regular expressions. I assume
    the default behavior will be improved with Ruby 2.0, but I'm not using
    1.9 so can't say for sure.

    > - mingling literal " / \ etc, with their regex counterparts, in ways
    > that would be daunting for the inexperienced

    The first think to keep in mind is that it never hurts to accidentally
    escape something in a double quoted (soft quoted) string or regex. So if
    you aren't sure, "\"", "\'", "\\" are all okay, as are /\"/, /\//, and
    %r|\/| (the latter being an alternative way to specify a regex. But you
    only need to escape characters that have special meaning. So in a
    slash-delimited regex, a slash has special meaning, but in a %r regex,
    it does not:
    %{/} is the same as /\//, as the former does not need to be escaped.

    If you use Regexp.new(" ... "), then the regexp comes from a string, and
    needs to follow the escaping rules for strings--you need to escape
    double quotes.

    A single quoted string is sometimes called "hard quoted". This means
    nothing is expanded / nothing has special meaning, so nothing needs to
    be escaped. Slash is not an escape character, here. The one exception is
    if the slash is before a single quote, in which case it will escape it.

    Sorry if these rules are confusing. You will get used to them. The way
    to learn regular expressions is to use them. You will get comfortable
    with them when you need them.

    > - just generally "higher-level" regex, leave the "intro to regex" to
    > all the other places. that's easy enough to find.
    >
    >


    Here's one of mine:
    /<a[^>]+?href=['"]?(.+?)['"\s>][^>]*>/im
    This matches a link. Throughout the regex I use [^>] frequently, which
    means "any character that does not end the tag". Think of [^>]* as a
    better .*
    Interesting bits:
    -using +? says that the match is non-greedy. It will match as little as
    possible. *? does the same think, but I find less use for it, as it
    usually matches an empty string.
    -the /i and /m at the end mean "case insensitive" and "multi-line". You
    can mix and match from /i, /m, /x (extended--ignores whitespace in the
    regex).

    I don't know what your level is, so this may be a bit too cryptic, but
    you can probably puzzle it out if you are complaining about regex
    tutorials being too basic.

    Dan
     
    Dan Zwell, Aug 16, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Peter D. Dunlap

    Configuration Error Not Helpful

    Peter D. Dunlap, Jul 18, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    363
    Peter D. Dunlap
    Jul 19, 2005
  2. =?Utf-8?B?cm9nZXIyMw==?=

    MSbuild not building. no helpful error messages.

    =?Utf-8?B?cm9nZXIyMw==?=, May 27, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    4,678
    =?Utf-8?B?cm9nZXIyMw==?=
    May 27, 2006
  3. Pettersen, Bjorn S
    Replies:
    1
    Views:
    1,864
    Emile van Sebille
    Nov 11, 2003
  4. Possum

    Helpful CSS New Group

    Possum, Oct 17, 2006, in forum: HTML
    Replies:
    1
    Views:
    393
    carolyn
    Oct 17, 2006
  5. metaperl
    Replies:
    2
    Views:
    303
    Gabriel Genellina
    Jan 13, 2007
Loading...

Share This Page