1

I have this line which does what its supposed to do:

Get-Content $OutFax\$newName |ForEach-Object {$_ -replace "a", "b" }| Set-Content $OutFax\$txtName

Opens a file and replaces a with b. But in the real world a is:

<ToFaxNum:11111111>

I would like to replace that with

::11111111

The ::11111111 could be any number between 1 and 9 and from 8 to 15 numbers. After ToFaxNum:11111111 there is a new line.

I have not tried anything because i have absolutely no idea how to solve it. I dont understand regex.

Thank you.

5
  • this is what you are looking for ToFaxNum(:\d{8,15}) see demo regex101.com/r/kY6gR2/3 Commented Oct 18, 2015 at 16:28
  • Thank you. The actual text that needs replacing does have <> around it. Commented Oct 18, 2015 at 16:32
  • then this would work out fine <ToFaxNum(:\d{8,15})>. See the new demo regex101.com/r/kY6gR2/4 Commented Oct 18, 2015 at 16:35
  • Do you mind if i post it as an answer Commented Oct 18, 2015 at 16:37
  • Please do, thank you. Commented Oct 18, 2015 at 19:15

2 Answers 2

2

The -replace operator supports capturing, just enclose your pattern in () in the first argument:

PS C:\> "<ToFaxNum:14435242624>" -replace '<ToFaxNum:(\d{8,15})>$','::$1'
::14435242624

\d{8,15} means between 8 and 15 consecutive digits. We can then refer to this captured group as $1 in the second argument.

Sign up to request clarification or add additional context in comments.

Comments

1

This would work out fine

<ToFaxNum(:\d{8,15})>

Then replace the matched string with :\1

EXPLANATION

<ToFaxNum: matches less than sign < followed by <ToFaxNum

(:\d{8,15}): matches and captures a colon : followed by at least 8 digits and at most 15 digits

>: matches the greater than sign >

check out the demo

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.