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. Need some help with regular expression

Need some help with regular expression

Scheduled Pinned Locked Moved Regular Expressions
regexhelptutorialquestion
10 Posts 3 Posters 9 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.
  • M Offline
    M Offline
    Member_15249095
    wrote on last edited by
    #1

    I am trying to match the following sample patterns: - 1 - 23-4B - 2,1-12 - 15A - 2-5,12 - 12A-4 so I tried the following regex but it seems it only matches some of above patterns... ^\d+[A-Z]?(-|,)?\d+[A-Z]?(-|,)?\d+[A-Z]?$ For example, the above matches all samples but not - 1 - 15A - 2,5 What did I miss here...

    M L 3 Replies Last reply
    0
    • M Member_15249095

      I am trying to match the following sample patterns: - 1 - 23-4B - 2,1-12 - 15A - 2-5,12 - 12A-4 so I tried the following regex but it seems it only matches some of above patterns... ^\d+[A-Z]?(-|,)?\d+[A-Z]?(-|,)?\d+[A-Z]?$ For example, the above matches all samples but not - 1 - 15A - 2,5 What did I miss here...

      M Offline
      M Offline
      Member_15245024
      wrote on last edited by
      #2

      Hi! :) Try this: ^\d*(-|,)?\w*(-|,)?\w*$ They all start with a indefinite digit at the beginning of string: ^\d* In the middle there is optional characters: (-|,)? Ending with either digit or letter: \w* Hope it helps. Nice day! :thumbsup:

      M 1 Reply Last reply
      0
      • M Member_15245024

        Hi! :) Try this: ^\d*(-|,)?\w*(-|,)?\w*$ They all start with a indefinite digit at the beginning of string: ^\d* In the middle there is optional characters: (-|,)? Ending with either digit or letter: \w* Hope it helps. Nice day! :thumbsup:

        M Offline
        M Offline
        Member_15249095
        wrote on last edited by
        #3

        Thank you for the reply. However, the above expression doesn't match the following test cases: -2C-5,3 -2,1-12 -2-5,12

        M 1 Reply Last reply
        0
        • M Member_15249095

          I am trying to match the following sample patterns: - 1 - 23-4B - 2,1-12 - 15A - 2-5,12 - 12A-4 so I tried the following regex but it seems it only matches some of above patterns... ^\d+[A-Z]?(-|,)?\d+[A-Z]?(-|,)?\d+[A-Z]?$ For example, the above matches all samples but not - 1 - 15A - 2,5 What did I miss here...

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          You have six different patterns to match so regex is probably not the optimum choice of tool.

          M 1 Reply Last reply
          0
          • M Member_15249095

            Thank you for the reply. However, the above expression doesn't match the following test cases: -2C-5,3 -2,1-12 -2-5,12

            M Offline
            M Offline
            Member_15245024
            wrote on last edited by
            #5

            Hi! You did not list them from the beginning. But, as you do say about them now, it is matching: ^\d*(\w+)?(-|,)?\w*(-|,)?\w*$ Have a nice day!

            1 Reply Last reply
            0
            • L Lost User

              You have six different patterns to match so regex is probably not the optimum choice of tool.

              M Offline
              M Offline
              Member_15245024
              wrote on last edited by
              #6

              Hello, sir! Your above comment intrigued me and made me curious. Would you care to share something or a link that would help me what would a optimum choice of tool for over six different patterns be. I want to learn to optimize my work. Thank you, sir!

              L 1 Reply Last reply
              0
              • M Member_15245024

                Hello, sir! Your above comment intrigued me and made me curious. Would you care to share something or a link that would help me what would a optimum choice of tool for over six different patterns be. I want to learn to optimize my work. Thank you, sir!

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #7

                Member 15245024 wrote:

                share something or a link that would help me

                Yes, but I have no idea what problem you are trying to solve.

                M 1 Reply Last reply
                0
                • L Lost User

                  Member 15245024 wrote:

                  share something or a link that would help me

                  Yes, but I have no idea what problem you are trying to solve.

                  M Offline
                  M Offline
                  Member_15245024
                  wrote on last edited by
                  #8

                  Hello, sir! Yes. I have a case that I defined hopefully in a accurate way as being a case of various, simultaneous and multiple replacements. I do not even know if it is even possible. I have a text, a long one, and sprinkled all over the place in the text, in the beginning of the string, in the middle, at the end, in parenthesis or not, with such expressions of 8 kinds: Abc. 22 Defg 22 Hijkl 22 Mnoprs 22 2 Tuvx 22 2 Zcb 22 2 Fh 22 C.C. 22 I need to replace each of the above with these below: Abc 22 Def 22 Hij 22 Mno 22 2Tu 22 2Zc 22 2Fh 22 Cal 22 This is the kind of situation I have, but not exactly these letters and digits - these are a dummy text as an example. Is it possible to do this simultaneously in parallel and in the same time? What would you use? THANK YOU, sir.

                  L 1 Reply Last reply
                  0
                  • M Member_15245024

                    Hello, sir! Yes. I have a case that I defined hopefully in a accurate way as being a case of various, simultaneous and multiple replacements. I do not even know if it is even possible. I have a text, a long one, and sprinkled all over the place in the text, in the beginning of the string, in the middle, at the end, in parenthesis or not, with such expressions of 8 kinds: Abc. 22 Defg 22 Hijkl 22 Mnoprs 22 2 Tuvx 22 2 Zcb 22 2 Fh 22 C.C. 22 I need to replace each of the above with these below: Abc 22 Def 22 Hij 22 Mno 22 2Tu 22 2Zc 22 2Fh 22 Cal 22 This is the kind of situation I have, but not exactly these letters and digits - these are a dummy text as an example. Is it possible to do this simultaneously in parallel and in the same time? What would you use? THANK YOU, sir.

                    L Offline
                    L Offline
                    Lost User
                    wrote on last edited by
                    #9

                    Because of all the differences I think you probably need to use a translate table. Create two lists of strings, the first is the strings to search for, and the second is the matching replacement. You would then need to go through each table for each line of text looking for strings in table 1. For each match you need to replace it with the appropriate entry in table 2. I am not sure how you could do this in parallel processing as it is really a sequential process.

                    1 Reply Last reply
                    0
                    • M Member_15249095

                      I am trying to match the following sample patterns: - 1 - 23-4B - 2,1-12 - 15A - 2-5,12 - 12A-4 so I tried the following regex but it seems it only matches some of above patterns... ^\d+[A-Z]?(-|,)?\d+[A-Z]?(-|,)?\d+[A-Z]?$ For example, the above matches all samples but not - 1 - 15A - 2,5 What did I miss here...

                      L Offline
                      L Offline
                      Lost User
                      wrote on last edited by
                      #10

                      If this matches to much, I would need more samples to know what 'should' and 'should not' be matched... ^\d{1,2}[-,A-Z\d]{0,5}$

                      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