Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Regular Expressions...

Regular Expressions...

Scheduled Pinned Locked Moved C#
question
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    simplicitylabs
    wrote on last edited by
    #1

    I'm having trouble getting my head around regular expressions. How do I find anything between < and > ? Thank you.

    J 1 Reply Last reply
    0
    • S simplicitylabs

      I'm having trouble getting my head around regular expressions. How do I find anything between < and > ? Thank you.

      J Offline
      J Offline
      J Dunlap
      wrote on last edited by
      #2

      The following regex will return the contents of anything between < and > in a named capture called "TagContents":

      \<(?<TagContents>[^\>]*)\>

      Here's how it is constructed: An escaped < character: \< An escaped "<" character - escaped to prevent it from being treated as a special expression character. This < matches the beginning of the tag. A named capture group: (?<name>matchexpr) Matches the matchexpr expression and returns it in a named capture called name. A custom character class: [^\>]* Custom character classes are defined by having a set of character identifiers within square brackets [ ]. The ^ character at the beginning means that the character class should match characters that are not contained within the characters listed within the brackets. So, this character class will match characters that are not ">". The asterisk at the end is a wildcard character that indicates that the character class should be matched any number of times possible, from 0 to the remaining length of the string. An escaped > character: \> Matches the end of the tag.

      --Justin Microsoft MVP, C#

      C# / Web / VG.net / MyXaml expert currently looking for (telecommute) contract work![^]

      S 1 Reply Last reply
      0
      • J J Dunlap

        The following regex will return the contents of anything between < and > in a named capture called "TagContents":

        \<(?<TagContents>[^\>]*)\>

        Here's how it is constructed: An escaped < character: \< An escaped "<" character - escaped to prevent it from being treated as a special expression character. This < matches the beginning of the tag. A named capture group: (?<name>matchexpr) Matches the matchexpr expression and returns it in a named capture called name. A custom character class: [^\>]* Custom character classes are defined by having a set of character identifiers within square brackets [ ]. The ^ character at the beginning means that the character class should match characters that are not contained within the characters listed within the brackets. So, this character class will match characters that are not ">". The asterisk at the end is a wildcard character that indicates that the character class should be matched any number of times possible, from 0 to the remaining length of the string. An escaped > character: \> Matches the end of the tag.

        --Justin Microsoft MVP, C#

        C# / Web / VG.net / MyXaml expert currently looking for (telecommute) contract work![^]

        S Offline
        S Offline
        simplicitylabs
        wrote on last edited by
        #3

        I'm sorry, I asked the wrong question. I actually need to find the entire tag, including the < and >.

        J 1 Reply Last reply
        0
        • S simplicitylabs

          I'm sorry, I asked the wrong question. I actually need to find the entire tag, including the < and >.

          J Offline
          J Offline
          J Dunlap
          wrote on last edited by
          #4

          Then just put the \< and \> inside the capture group:

          (?<TagContents>\<[^\>]*\>)

          ...so that they will be captured along with what's between them.

          --Justin Microsoft MVP, C#

          C# / Web / VG.net / MyXaml expert currently looking for (telecommute) contract work![^]

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups