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. Replace certain characters in part of string?

Replace certain characters in part of string?

Scheduled Pinned Locked Moved Regular Expressions
regexquestionhelptutorial
5 Posts 2 Posters 14 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.
  • W Offline
    W Offline
    w 3g
    wrote on last edited by
    #1

    Hello, I have a question on regex: Is it possible to match for certain characters (and replace them) in only a part of a string? E. g. str="I want to solve this problem" I now want to match all spaces after the first occurence of the letter, say, "v". I know I can filter the whole part of the string after (and including) the first v with the regex "v.*", but how to match the spaces only in that part? Thank you!

    Richard DeemingR 1 Reply Last reply
    0
    • W w 3g

      Hello, I have a question on regex: Is it possible to match for certain characters (and replace them) in only a part of a string? E. g. str="I want to solve this problem" I now want to match all spaces after the first occurence of the letter, say, "v". I know I can filter the whole part of the string after (and including) the first v with the regex "v.*", but how to match the spaces only in that part? Thank you!

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

      Depends on the regex engine you're using. For example, in C#, you can use a zero-width positive look-behind assertion[^]:

      Regex re = new Regex("(?<=.*v.*)\s+");
      string[] parts = re.Split("I want to solve this problem");
      // parts === { "I want to solve", "this", "problem" }


      "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

      W 1 Reply Last reply
      0
      • Richard DeemingR Richard Deeming

        Depends on the regex engine you're using. For example, in C#, you can use a zero-width positive look-behind assertion[^]:

        Regex re = new Regex("(?<=.*v.*)\s+");
        string[] parts = re.Split("I want to solve this problem");
        // parts === { "I want to solve", "this", "problem" }


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

        W Offline
        W Offline
        w 3g
        wrote on last edited by
        #3

        Thanks, Richard, for your answer! I am using regex with javascript, developing for popular browsers. So, if I understand correctly, your code splits the string into 3 parts, the first part being everything until the first space after the first "v", the other parts being the rest of the string split by spaces. I am sure that will work. Well, what I was looking for - this is a more theoretical and general question - is there an operator you can use in regexes which does something like "apply the following only to the previously matched part"? For example: /v.*#c/g Explanation: The first part of the regex is "v.*" which means: Match the part of the string after the first v. The second part is "c", which will match every c. Now, is there an operator (here symolized by #) between the 2 parts of the regex, which means: "Apply the part after this operator ("c") to the result of the part before the operator ("v.*)"? It is like: Do a secondary match inside the primary match. Sorry, English is not my native language, but I still hope, I could make my point clear. Thank you.

        Richard DeemingR 1 Reply Last reply
        0
        • W w 3g

          Thanks, Richard, for your answer! I am using regex with javascript, developing for popular browsers. So, if I understand correctly, your code splits the string into 3 parts, the first part being everything until the first space after the first "v", the other parts being the rest of the string split by spaces. I am sure that will work. Well, what I was looking for - this is a more theoretical and general question - is there an operator you can use in regexes which does something like "apply the following only to the previously matched part"? For example: /v.*#c/g Explanation: The first part of the regex is "v.*" which means: Match the part of the string after the first v. The second part is "c", which will match every c. Now, is there an operator (here symolized by #) between the 2 parts of the regex, which means: "Apply the part after this operator ("c") to the result of the part before the operator ("v.*)"? It is like: Do a secondary match inside the primary match. Sorry, English is not my native language, but I still hope, I could make my point clear. Thank you.

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

          Look-behind is the way to go:

          Lookahead and lookbehind[^]

          (?<=Y)X matches X, but only if there's Y before it

          Demo[^] It's supported in the Javascript regex engine for most modern browsers: Can I use... Lookbehind in JS regular expressions[^] The only hold-outs are Internet Explorer - which even Microsoft agree should not be used any more - and Safari.


          "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

          W 1 Reply Last reply
          0
          • Richard DeemingR Richard Deeming

            Look-behind is the way to go:

            Lookahead and lookbehind[^]

            (?<=Y)X matches X, but only if there's Y before it

            Demo[^] It's supported in the Javascript regex engine for most modern browsers: Can I use... Lookbehind in JS regular expressions[^] The only hold-outs are Internet Explorer - which even Microsoft agree should not be used any more - and Safari.


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

            W Offline
            W Offline
            w 3g
            wrote on last edited by
            #5

            Great, that was exactly what i was looking for, thank you so much, Richard! So, to solve the problem from my original posting, that is to match all spaces after the first "v" in the string "I want to solve this problem", this Regex works:

            /(?<=v.*) /g

            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