hi all,
I have a string like this;
"line 1
line 2
# replace starts here
# replace ends here
line 6
line 7
"
how can i use a regular expression to replace all that is between start and end in this string?
thank you!
replace part of string with regular expression
Forum rules
Do not post any licensing information in this forum.
Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
Do not post any licensing information in this forum.
Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
Re: replace part of string with regular expression
Replacing random text in a file with a regular expression is very difficult. We must have the exact conditions of the file contents at that location.
The basic technique is to do a multi\line trap then use that to replace the target.
Without a clear example this would be hard to describe.
The basic technique is to do a multi\line trap then use that to replace the target.
Without a clear example this would be hard to describe.
Re: replace part of string with regular expression
Here is one simple way to do this:
Code: Select all
$txt = "line 1
line 2
# replace starts here
# replace ends here
line 6
line 7
"
$txt -match '(?smi)(.*line 2)'
$p1 = $matches[1]
$txt -match '(?smi)(line 6.*)'
$p2 = $matches[1]
$p1 + $p2
Re: replace part of string with regular expression
i don't understand your replied code.
i said i wish to replace what is between start and end, why don't you look for that string instead of line 2?
line 2 is only there to show an example, it has nothing to do with the file's content.
my file contains all the required functions to do stuff i want, my GUI that generates the function calls is what i need to insert in the file, between those 2 flags i have in my text file.
anyway, I'll dig it up and will have an answer, it will only be longer.
thanks!
i said i wish to replace what is between start and end, why don't you look for that string instead of line 2?
line 2 is only there to show an example, it has nothing to do with the file's content.
my file contains all the required functions to do stuff i want, my GUI that generates the function calls is what i need to insert in the file, between those 2 flags i have in my text file.
anyway, I'll dig it up and will have an answer, it will only be longer.
thanks!
Re: replace part of string with regular expression
yes you're right. sorry, I guess I'm tired and should stop for the day.
thanks, man!
thanks, man!
Re: replace part of string with regular expression
Your eyes and ind need refreshing. RegEx is a challenge for non-programmers and many programmers. Text processing is a special area of computing. Understanding the issues is hard for anyone until the formalities are understood. Normal techs generally aren't called on for this kind of issue but, unfortunately, many managers fail to understand the technologies they manage.
Good luck and give it a try tomorrow. Post back if you have a question. Once you get your feet wet you will ask better questions that should be easier to answer.
Good luck and give it a try tomorrow. Post back if you have a question. Once you get your feet wet you will ask better questions that should be easier to answer.