regular expression

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Bruno Storz

    regular expression

    Hi there,

    I have strings like

    asdf asdf asdf "test.gif" blabla
    or
    asdf asdf asdf 'test.gif' blabla

    I want to extract test.gif. Because I will see " or ' in the string, I was
    (so far) unable to create a regular expression for this problem

    Regards

    Bruno


  • Martin Honnen

    #2
    Re: regular expression



    Bruno Storz wrote:

    [color=blue]
    > I have strings like
    >
    > asdf asdf asdf "test.gif" blabla
    > or
    > asdf asdf asdf 'test.gif' blabla
    >
    > I want to extract test.gif. Because I will see " or ' in the string, I was
    > (so far) unable to create a regular expression for this problem[/color]

    Here is an example

    var strings = [
    'asdf asdf asdf "test.gif" blabla',
    'asdf asdf asdf \'test.gif\' blabla',
    'asdf sdasd asdf adfadf'
    ];
    var pattern = /(["'])(\w+\.gif)(\1)/;
    for (var i = 0; i < strings.length; i++) {
    var match = pattern.exec(st rings[i]);
    if (match) {
    alert('found ' + match[2]);
    }
    else {
    alert(pattern + ' doesn\'t match ' + strings[i]);
    }
    }

    the pattern (["'])(\w+\.gif)(\1) looks for something.gif delimited by
    double or single quotes

    --

    Martin Honnen


    Comment

    • mscir

      #3
      Re: regular expression

      Martin Honnen wrote:
      [color=blue]
      > Bruno Storz wrote:[color=green]
      >> I have strings like
      >> asdf asdf asdf "test.gif" blabla
      >> or asdf asdf asdf 'test.gif' blabla
      >> I want to extract test.gif. Because I will see " or ' in the string, I
      >> was (so far) unable to create a regular expression for this problem[/color][/color]
      [color=blue]
      > Here is an example
      > var strings = [
      > 'asdf asdf asdf "test.gif" blabla',
      > 'asdf asdf asdf \'test.gif\' blabla',
      > 'asdf sdasd asdf adfadf'
      > ];
      > var pattern = /(["'])(\w+\.gif)(\1)/;
      > for (var i = 0; i < strings.length; i++) {
      > var match = pattern.exec(st rings[i]);
      > if (match) {
      > alert('found ' + match[2]);
      > }
      > else {
      > alert(pattern + ' doesn\'t match ' + strings[i]);
      > }
      > }
      > the pattern (["'])(\w+\.gif)(\1) looks for something.gif delimited by
      > double or single quotes[/color]

      Nice job. Can you recommend any sites you like about learning to use
      regular expressions?

      Mike

      Comment

      • Lasse Reichstein Nielsen

        #4
        Re: regular expression

        "Bruno Storz" <bruno_storz@gm x.de> writes:
        [color=blue]
        > I have strings like
        >
        > asdf asdf asdf "test.gif" blabla
        > or
        > asdf asdf asdf 'test.gif' blabla
        >
        > I want to extract test.gif. Because I will see " or ' in the string, I was
        > (so far) unable to create a regular expression for this problem[/color]


        This will do that for new browsers:

        var re = /(['"])([^\s]*?)\1/;
        var match = re.exec(someStr ing);
        var file = match && match[2];

        It only matches one file name per string, and the file name cannot
        contain a space.
        The omission of spaces is to avoid apostrophes occuring in the
        surrounding text, e.g.:
        So I can't download the file 'foo.bar', it's broken

        Only recent browsers accept non-greedy matching and back references
        in regular expressions.

        Another, more cumbersome version (although not as much as I expected,
        I see now that I wrote it) is:

        var re = /"([^"]*)"|'([^'\s]*)'/;
        var match = re.exec(someStr ing);
        var file = match && (match[1] || match[2]);

        It should work in most browsers.

        /L
        --
        Lasse Reichstein Nielsen - lrn@hotpop.com
        DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
        'Faith without judgement merely degrades the spirit divine.'

        Comment

        • Thomas 'PointedEars' Lahn

          #5
          Re: regular expression

          mscir wrote:
          [color=blue]
          > [...] Can you recommend any sites you like about learning to use
          > regular expressions?[/color]

          Besides
          <http://devedge.netscap e.com/library/manuals/2000/javascript/1.5/reference/regexp.html>
          and <http://php.net/pcre>, (sample) chapter 4 of
          <http://www.oreilly.com/catalog/regex/> helped me a lot.
          Most certainly the entire book can be recommended.


          HTHU2 :)

          PointedEars

          Comment

          • mscir

            #6
            Re: regular expression



            Thomas 'PointedEars' Lahn wrote:[color=blue]
            > mscir wrote:
            >
            >[color=green]
            >>[...] Can you recommend any sites you like about learning to use
            >>regular expressions?[/color]
            >
            >
            > Besides
            > <http://devedge.netscap e.com/library/manuals/2000/javascript/1.5/reference/regexp.html>
            > and <http://php.net/pcre>, (sample) chapter 4 of
            > <http://www.oreilly.com/catalog/regex/> helped me a lot.
            > Most certainly the entire book can be recommended.[/color]
            [color=blue]
            > HTHU2 :)
            > PointedEars[/color]

            Thanks,
            Mike

            Comment

            Working...