Yet another Regexp question

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

    Yet another Regexp question

    I'm trying to get the regular expression which matches text between two
    underscores:
    _MATCHTHIS_

    The problem is, I don't want to match it if the only thing in between
    the underscores are spaces or other underscores:
    _ _ _
    (this should get no match)

    _hello world_
    (this should match "hello world" even though there's a space because
    there's also things besides a space and underscore)

    Right now, I've got this:
    /_([^_]+?)_/ which does well at excluding _

    I've also tried this:
    /_([^_\s]+?)_/ which does well if there are NO spaces between the _'s.
    ie:
    _hello_
    (correctly matches "hello")
    _hello world_
    (does not match "hello world" as I would like)
    _ _ _
    (correctly does not match this)

    I'm stumped. Any help?

  • Rik

    #2
    Re: Yet another Regexp question

    HaggMan wrote:
    I'm trying to get the regular expression which matches text between
    two underscores:
    _MATCHTHIS_
    >
    The problem is, I don't want to match it if the only thing in between
    the underscores are spaces or other underscores:
    _ _ _
    (this should get no match)
    >
    _hello world_
    (this should match "hello world" even though there's a space because
    there's also things besides a space and underscore)
    >
    Right now, I've got this:
    /_([^_]+?)_/ which does well at excluding _
    >
    I've also tried this:
    /_([^_\s]+?)_/ which does well if there are NO spaces between the _'s.
    ie:
    _hello_
    (correctly matches "hello")
    _hello world_
    (does not match "hello world" as I would like)
    _ _ _
    (correctly does not match this)
    /_(\s*(:?[^_\s]+\s*)+)_/

    Grtz,
    --
    Rik Wasmus


    Comment

    • Chung Leong

      #3
      Re: Yet another Regexp question

      HaggMan wrote:
      I'm trying to get the regular expression which matches text between two
      underscores:
      _MATCHTHIS_
      >
      The problem is, I don't want to match it if the only thing in between
      the underscores are spaces or other underscores:
      _ _ _
      (this should get no match)
      >
      _hello world_
      (this should match "hello world" even though there's a space because
      there's also things besides a space and underscore)
      >
      Right now, I've got this:
      /_([^_]+?)_/ which does well at excluding _
      >
      I've also tried this:
      /_([^_\s]+?)_/ which does well if there are NO spaces between the _'s.
      ie:
      _hello_
      (correctly matches "hello")
      _hello world_
      (does not match "hello world" as I would like)
      _ _ _
      (correctly does not match this)
      >
      I'm stumped. Any help?
      Is _hello_world_ a match or not? The following pattern might be what
      you need:

      /\b_\B(.+?)\B_\b/

      Boundary conditions are used here instead of conditions on what's
      inside.

      Comment

      • HaggMan

        #4
        Re: Yet another Regexp question

        Thank you to both of you! I'll try them out tonight.

        _hello_world_ should match "hello" but not "world_" though it should
        technically *never be used that way in what I'm doing.

        Chung Leong wrote:
        HaggMan wrote:
        I'm trying to get the regular expression which matches text between two
        underscores:
        _MATCHTHIS_

        The problem is, I don't want to match it if the only thing in between
        the underscores are spaces or other underscores:
        _ _ _
        (this should get no match)

        _hello world_
        (this should match "hello world" even though there's a space because
        there's also things besides a space and underscore)

        Right now, I've got this:
        /_([^_]+?)_/ which does well at excluding _

        I've also tried this:
        /_([^_\s]+?)_/ which does well if there are NO spaces between the _'s.
        ie:
        _hello_
        (correctly matches "hello")
        _hello world_
        (does not match "hello world" as I would like)
        _ _ _
        (correctly does not match this)

        I'm stumped. Any help?
        >
        Is _hello_world_ a match or not? The following pattern might be what
        you need:
        >
        /\b_\B(.+?)\B_\b/
        >
        Boundary conditions are used here instead of conditions on what's
        inside.

        Comment

        • Chung Leong

          #5
          Re: Yet another Regexp question

          HaggMan wrote:
          Thank you to both of you! I'll try them out tonight.
          >
          _hello_world_ should match "hello" but not "world_" though it should
          technically *never be used that way in what I'm doing.
          >
          Then my pattern won't work.

          Comment

          • HaggMan

            #6
            Re: Yet another Regexp question

            Well, Rik's worked. Thank you both!


            Chung Leong wrote:
            HaggMan wrote:
            Thank you to both of you! I'll try them out tonight.

            _hello_world_ should match "hello" but not "world_" though it should
            technically *never be used that way in what I'm doing.
            >
            Then my pattern won't work.

            Comment

            Working...