Regex: different options on different sections of the Regex?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?RXRoYW4gU3RyYXVzcw==?=

    Regex: different options on different sections of the Regex?

    Hi,
    I would like to create a regular expression which matches
    "Y" or "male". The hard part is that the "Y" must be capital, but the "male"
    should be case insensitive. I think I have seen a way to have Regex options
    apply differently throughout the expression, but I can't find it now. Does
    anyone know if this is possible or how to do it?
    Thanks!
    Ethan

    Ethan Strauss Ph.D.
    Bioinformatics Scientist
    Promega Corporation
    2800 Woods Hollow Rd.
    Madison, WI 53711
    608-274-4330
    800-356-9526
    ethan.strauss@p romega.com
  • Martin Honnen

    #2
    Re: Regex: different options on different sections of the Regex?

    Ethan Strauss wrote:
    I would like to create a regular expression which matches
    "Y" or "male". The hard part is that the "Y" must be capital, but the "male"
    should be case insensitive. I think I have seen a way to have Regex options
    apply differently throughout the expression, but I can't find it now. Does
    anyone know if this is possible or how to do it?
    I don't know of a way to apply options to different sections but with
    the few characters you could spell it out e.g.
    Y|[Mm][Aa][Ll][Ee]


    --

    Martin Honnen --- MVP XML

    Comment

    • =?Utf-8?B?RXRoYW4gU3RyYXVzcw==?=

      #3
      Re: Regex: different options on different sections of the Regex?

      I was hoping to be able to get fancy, but your solution will work at least in
      this case.
      Thanks,
      Ethan

      "Martin Honnen" wrote:
      Ethan Strauss wrote:
      >
      I would like to create a regular expression which matches
      "Y" or "male". The hard part is that the "Y" must be capital, but the "male"
      should be case insensitive. I think I have seen a way to have Regex options
      apply differently throughout the expression, but I can't find it now. Does
      anyone know if this is possible or how to do it?
      >
      I don't know of a way to apply options to different sections but with
      the few characters you could spell it out e.g.
      Y|[Mm][Aa][Ll][Ee]
      >
      >
      --
      >
      Martin Honnen --- MVP XML

      >

      Comment

      • Michael Bray

        #4
        Re: Regex: different options on different sections of the Regex?

        =?Utf-8?B?RXRoYW4gU3R yYXVzcw==?=
        <EthanStrauss@d iscussions.micr osoft.comwrote in
        news:0ACA35C2-94F2-4FFA-9546-4E6074A6FDB2@mi crosoft.com:
        I was hoping to be able to get fancy, but your solution will work at
        least in this case.
        You can dynamically change options within the .NET Regular expressions...
        This does what you want:

        Y|(?i-:male)

        You should check out Expresso - it is a fantastic utility for building and
        testing regular expressions.

        Expresso: http://www.ultrapico.com/Expresso.htm

        -mdb

        Comment

        • Martin Honnen

          #5
          Re: Regex: different options on different sections of the Regex?

          Ethan Strauss wrote:
          I was hoping to be able to get fancy, but your solution will work at least in
          this case.
          I have checked the documentation now and the .NET framework's regular
          expression language indeed allows turning on options with e.g. (?i)
          so this code

          foreach (string s in new string[] { "Y", "y", "Male", "mAle" })
          {
          Console.WriteLi ne(Regex.IsMatc h(s, "Y|(?i)male "));
          }

          outputs

          True
          False
          True
          True


          --

          Martin Honnen --- MVP XML

          Comment

          Working...