Replace certain characters in part of string?
-
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!
-
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!
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
-
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
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.
-
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.
Look-behind is the way to go:
(?<=Y)X
matchesX
, but only if there'sY
before itDemo[^] 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
-
Look-behind is the way to go:
(?<=Y)X
matchesX
, but only if there'sY
before itDemo[^] 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