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. Regular Expressions
  4. Matching exact string with a vertical bar and square brackets

Matching exact string with a vertical bar and square brackets

Scheduled Pinned Locked Moved Regular Expressions
regextutorialquestion
5 Posts 2 Posters 12 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.
  • R Offline
    R Offline
    RanCohen
    wrote on last edited by
    #1

    I want to match exact strings with a vertical bar and square brackets, but I want to match the shortest possible. Meaning, I want to match the string [aaa|bbb] not anything in this [aaa] | [bbb] or this [aaa] | [aaa|bbb] (in this example, I only want [aaa|bbb], and want the the first [aaa] be ignored (not matched) so I can't use the regex: \[.*?\|(.*?)] It can have non-alphabetical chars, like this [aaa|(ccc)bbb], or be in different languages. What can I do?

    Richard DeemingR 1 Reply Last reply
    0
    • R RanCohen

      I want to match exact strings with a vertical bar and square brackets, but I want to match the shortest possible. Meaning, I want to match the string [aaa|bbb] not anything in this [aaa] | [bbb] or this [aaa] | [aaa|bbb] (in this example, I only want [aaa|bbb], and want the the first [aaa] be ignored (not matched) so I can't use the regex: \[.*?\|(.*?)] It can have non-alphabetical chars, like this [aaa|(ccc)bbb], or be in different languages. What can I do?

      Richard DeemingR Offline
      Richard DeemingR Offline
      Richard Deeming
      wrote on last edited by
      #2

      How about:

      \[[^\]]*\|[^\]]*\]

      This will match:

      • [aaa|bbb]
      • [|bbb]
      • [aaa|]
      • [|]

      It will not match:

      • [aaa]
      • [aaa]|[bbb]

      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

      "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

      R 1 Reply Last reply
      0
      • Richard DeemingR Richard Deeming

        How about:

        \[[^\]]*\|[^\]]*\]

        This will match:

        • [aaa|bbb]
        • [|bbb]
        • [aaa|]
        • [|]

        It will not match:

        • [aaa]
        • [aaa]|[bbb]

        "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

        R Offline
        R Offline
        RanCohen
        wrote on last edited by
        #3

        Doesn't seem to work. Using this engine it only matched the first line: a [aaa|bbb] b a [aaa|bbb] b [|bbb] [aaa|] [|] [aaa] [aaa]|[bbb]

        Richard DeemingR 1 Reply Last reply
        0
        • R RanCohen

          Doesn't seem to work. Using this engine it only matched the first line: a [aaa|bbb] b a [aaa|bbb] b [|bbb] [aaa|] [|] [aaa] [aaa]|[bbb]

          Richard DeemingR Offline
          Richard DeemingR Offline
          Richard Deeming
          wrote on last edited by
          #4

          That site finds all five matches for me. Demo[^] Screenshot[^]


          "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

          "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

          R 1 Reply Last reply
          0
          • Richard DeemingR Richard Deeming

            That site finds all five matches for me. Demo[^] Screenshot[^]


            "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

            R Offline
            R Offline
            RanCohen
            wrote on last edited by
            #5

            Needed to turn on the global flag... Thank you!

            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