replace part of string with regular expression

Ask your PowerShell-related questions, including questions on cmdlet development!
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.
Locked
User avatar
Domtar
Posts: 90
Joined: Sun Mar 03, 2013 12:42 pm

replace part of string with regular expression

Post by Domtar »

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!

jvierra
Posts: 14480
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: replace part of string with regular expression

Post by jvierra »

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.

jvierra
Posts: 14480
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: replace part of string with regular expression

Post by jvierra »

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

User avatar
Domtar
Posts: 90
Joined: Sun Mar 03, 2013 12:42 pm

Re: replace part of string with regular expression

Post by Domtar »

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!

jvierra
Posts: 14480
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: replace part of string with regular expression

Post by jvierra »

You can match on anything you want. I just gave you a simple example.

User avatar
Domtar
Posts: 90
Joined: Sun Mar 03, 2013 12:42 pm

Re: replace part of string with regular expression

Post by Domtar »

yes you're right. sorry, I guess I'm tired and should stop for the day.

thanks, man!

jvierra
Posts: 14480
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: replace part of string with regular expression

Post by jvierra »

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.

Locked