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. how to write regular expression for this code?

how to write regular expression for this code?

Scheduled Pinned Locked Moved C#
questionregextutorial
14 Posts 6 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.
  • OriginalGriffO OriginalGriff

    You can't - your code is wrong, on a number of levels. Firstly:

    if(!identier.StartsWith("L#")
    identifier.Replace(something,"");

    Is incomplete - the if condition is never terminated (you need a ')' in there. (Assuming you fix that) Secondly: If identifier starts with "L#" then it won't start with "X#". So, your replace operation will be called by at least one of the first two if conditions, and then always by the third. (Assuming you fix that) Thirdly: Strings in .NET are immutable - they cannot be changed - so string.Replace does nothing at all to identifier, but instead generates a new string, which you then discard. As a result, your entire code block as shown can be replaced by nothing, as it has no effect whatsoever on subsequent code! You don't need a regex for that! Think again what you are trying to do, and perhaps explain to us what results you are expecting, becauae that is not going to give them to you. :laugh:

    Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together. Manfred R. Bihy: "Looks as if OP is learning resistant."

    P Offline
    P Offline
    PIEBALDconsult
    wrote on last edited by
    #4

    Certainly your first point and conclusion are correct, but the other points assume a String, which may not be a safe assumption. identifier may be a StringBuilder (for instance*) and the StartsWith method may be an Extension Method -- we just don't know, not enough information was provided. If in fact your third point is incorrect, and identifier is mutable, then your second point is also not valid. Having said that, I wouldn't bet against your being absolutely correct on all points. :sigh: * It could also be some other unknown class.

    OriginalGriffO B 2 Replies Last reply
    0
    • P PIEBALDconsult

      Certainly your first point and conclusion are correct, but the other points assume a String, which may not be a safe assumption. identifier may be a StringBuilder (for instance*) and the StartsWith method may be an Extension Method -- we just don't know, not enough information was provided. If in fact your third point is incorrect, and identifier is mutable, then your second point is also not valid. Having said that, I wouldn't bet against your being absolutely correct on all points. :sigh: * It could also be some other unknown class.

      OriginalGriffO Offline
      OriginalGriffO Offline
      OriginalGriff
      wrote on last edited by
      #5

      I agree with you completely - it's just Occam suggests my answer, given the code quality of the whole fragment :sigh:

      Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together. Manfred R. Bihy: "Looks as if OP is learning resistant."

      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
      "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

      B 1 Reply Last reply
      0
      • S SRKSHOME

        hi, I have below code. how do I replace with regular expression or much sipler to this.? many thanks

        if(!identier.StartsWith("L#")
        identifier.Replace(something,"");
        if(!identier.StartsWith("X#")
        identifier.Replace(something,"");
        if(!identier.StartsWith("G#")
        identifier.Replace(something,"");

        L Offline
        L Offline
        Luc Pattyn
        wrote on last edited by
        #6

        It depends on the declaration of identifier. Assuming it is a string, here is my shortest replacement:

        new System.Text.RegularExpressions.Regex(identifier.ToString());

        Others already explained why it does the same your code does. And yes, the ToString() part is necessary. :)

        Luc Pattyn [My Articles] Nil Volentibus Arduum

        P 1 Reply Last reply
        0
        • L Luc Pattyn

          It depends on the declaration of identifier. Assuming it is a string, here is my shortest replacement:

          new System.Text.RegularExpressions.Regex(identifier.ToString());

          Others already explained why it does the same your code does. And yes, the ToString() part is necessary. :)

          Luc Pattyn [My Articles] Nil Volentibus Arduum

          P Offline
          P Offline
          PIEBALDconsult
          wrote on last edited by
          #7

          Luc Pattyn wrote:

          And yes, the ToString() part is necessary.

          Ummm... what? :confused:

          L 1 Reply Last reply
          0
          • P PIEBALDconsult

            Luc Pattyn wrote:

            And yes, the ToString() part is necessary.

            Ummm... what? :confused:

            L Offline
            L Offline
            Luc Pattyn
            wrote on last edited by
            #8

            Exactly what I said. ;P

            Luc Pattyn [My Articles] Nil Volentibus Arduum

            1 Reply Last reply
            0
            • B BillWoodruff

              Looking at your example, the "pattern" that "jumps out at me" is that in each case you are using the first two characters of (what I assume is the string) 'identifier.' That suggests, to me, a switch/case statement using the first two characters of the 'identifier' string as the 'match' criterion. But, if your 'heart is set' on a RegEx solution, don't let me deter you :) best, Bill

              "Beauty is in the eye of the beholder, and it may be necessary from time to time to give a stupid or misinformed beholder a black eye." Miss Piggy"

              S Offline
              S Offline
              SRKSHOME
              wrote on last edited by
              #9

              it was my mistake..yes...identifier is a string type.

              1 Reply Last reply
              0
              • OriginalGriffO OriginalGriff

                You can't - your code is wrong, on a number of levels. Firstly:

                if(!identier.StartsWith("L#")
                identifier.Replace(something,"");

                Is incomplete - the if condition is never terminated (you need a ')' in there. (Assuming you fix that) Secondly: If identifier starts with "L#" then it won't start with "X#". So, your replace operation will be called by at least one of the first two if conditions, and then always by the third. (Assuming you fix that) Thirdly: Strings in .NET are immutable - they cannot be changed - so string.Replace does nothing at all to identifier, but instead generates a new string, which you then discard. As a result, your entire code block as shown can be replaced by nothing, as it has no effect whatsoever on subsequent code! You don't need a regex for that! Think again what you are trying to do, and perhaps explain to us what results you are expecting, becauae that is not going to give them to you. :laugh:

                Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together. Manfred R. Bihy: "Looks as if OP is learning resistant."

                S Offline
                S Offline
                SRKSHOME
                wrote on last edited by
                #10

                my requirement is if a string starts with "L#",or "G#" or "X#" i dont want to replace some part of the string. Otherwise I will replace. for example: string identifier="L#1234"; In this case I dont want to replace any thing there. Otherewise I would replace some part of the string there.

                P 1 Reply Last reply
                0
                • S SRKSHOME

                  my requirement is if a string starts with "L#",or "G#" or "X#" i dont want to replace some part of the string. Otherwise I will replace. for example: string identifier="L#1234"; In this case I dont want to replace any thing there. Otherewise I would replace some part of the string there.

                  P Offline
                  P Offline
                  Pete OHanlon
                  wrote on last edited by
                  #11

                  Then why worry about using a regular expression? There's no need to do this - sometimes it's just easier to directly manipulate a string - in this case, if the second character isn't hash then you don't need to check the first character.

                  Forgive your enemies - it messes with their heads

                  My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility

                  S 1 Reply Last reply
                  0
                  • P Pete OHanlon

                    Then why worry about using a regular expression? There's no need to do this - sometimes it's just easier to directly manipulate a string - in this case, if the second character isn't hash then you don't need to check the first character.

                    Forgive your enemies - it messes with their heads

                    My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility

                    S Offline
                    S Offline
                    SRKSHOME
                    wrote on last edited by
                    #12

                    got it..thanks. sometimes we think too much for simple ones.

                    1 Reply Last reply
                    0
                    • OriginalGriffO OriginalGriff

                      I agree with you completely - it's just Occam suggests my answer, given the code quality of the whole fragment :sigh:

                      Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together. Manfred R. Bihy: "Looks as if OP is learning resistant."

                      B Offline
                      B Offline
                      BillWoodruff
                      wrote on last edited by
                      #13

                      +5 for bringing in my favorite "razor," William of Ockham, as well as, of course, a good, solid, technical answer. best, Bill

                      "Beauty is in the eye of the beholder, and it may be necessary from time to time to give a stupid or misinformed beholder a black eye." Miss Piggy"

                      1 Reply Last reply
                      0
                      • P PIEBALDconsult

                        Certainly your first point and conclusion are correct, but the other points assume a String, which may not be a safe assumption. identifier may be a StringBuilder (for instance*) and the StartsWith method may be an Extension Method -- we just don't know, not enough information was provided. If in fact your third point is incorrect, and identifier is mutable, then your second point is also not valid. Having said that, I wouldn't bet against your being absolutely correct on all points. :sigh: * It could also be some other unknown class.

                        B Offline
                        B Offline
                        BillWoodruff
                        wrote on last edited by
                        #14

                        +5 for acute analysis thoroughly applied ! best, Bill

                        "Beauty is in the eye of the beholder, and it may be necessary from time to time to give a stupid or misinformed beholder a black eye." Miss Piggy"

                        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