0

I've been reading a ton of material and thought I had found my solution but no luck. I need to find apostrophes contained in a name and then replace them with a double. I am loading a file to an array and then looping through that, looking for the apostrophes. The catch is that each row can have several apostrophes so that's why it's not a simple find and replace.

Here is a sample of the file:

   create(xxxxxxx)using(xxxxxxx)name('O'Doe, John')
   replace(xxxxxxx)instdata('ab 1234 ')
   create(xxxxxxx)using(xxxxxxx)name('Doe, O'Jane')
   replace(xxxxxxx)instdata('ab 5678 ')

There are other lines inbetween but they don't contain apostrophes.

Here is what I have so far:

    $Pattern = "[A-Z]'[A-Z]"
    $user = gc C:\Temp\mfnewuser.ins
    for ($i = 0; $i -lt $user.count; $i++) {
       if ($user[$i] -match $Pattern) {
       $user[$i] = [regex]::replace($strText, $Pattern.substring(2,1), "''")
       $user | out-file C:\Temp\mfnewuser.ins
       }
    }

I'm looking for a capital letter, followed by an apostrophe, followed by another capital. Because of the other commas, I can't just do a global replace. I know my pattern matching is working but I can't seem to manipulate it with the substring. The substring looks at $Pattern as a string instead of the result of a regex. If I can save the regex result to a variable, that would be great. I think then the replace would be easy.

Tried this as well but no luck either:

    $Pattern = "[A-Z]'[A-Z]"
    $NewPattern = "[A-Z]''[A-Z]"
    $f = Get-Content C:\Temp\mfnewuser.ins
    $f = $f -replace $Pattern, $NewPattern
    $f | out-file C:\Temp\mfnewuser.ins

I may be approaching this all wrong and there is an easier way but I haven't seen anything yet.

EDIT: Based on Bill_Stewarts example below, I've got this to work on the First Name but not yet the Last Name:

$Pattern = "[A-Z]'[A-Z]"
$user = gc C:\Temp\mfnewuser.ins
for ($i = 0; $i -lt $user.count; $i++) {
    if ($user[$i] -match $Pattern) {
        $user[$i] = $user[$i] -replace "(.*[A-Z])'([A-Z]+.*)", "`$1''`$2"
        $user | out-file C:\Temp\mfnewuser.ins
        }
    }
2
  • The regex is taylored to what forms you expect to see surrounding the apostrophe. Try one of @hwnd's regex's. Commented Oct 4, 2013 at 19:15
  • Looks like we got the same thing - ([A-Z])'([A-Z]) should be sufficient. Commented Oct 4, 2013 at 19:17

2 Answers 2

1

Perhaps something like this?

get-content "test.txt" | foreach-object {
  $_ -replace "([A-Z])'([A-Z])", "`$1''`$2"
}

Regular expressions can be grouped using ( ) and the -replace operator supports substring replacement ($1 and $2).

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

8 Comments

Thanks, I'll give this a shot. I've seen the $1 and $2 references before but wasn't sure what those were for. How do those work?
$0 means 'entire string that matched', $1 means 'first grouped subexpression', $2 means 'second grouped subexpression', and so forth.
Wouldn't ([A-Z])'([A-Z]) be sufficient?
That's enough for a match, but you need enough to do the replace.
OK, it kind of working. It doubles-up the single-quote on the first name but not on the last: ('O'Last, O''First'). I'm still new to regex and don't yet understand all of the symbols used.
|
1

Replace your line, with the following.

$user[$i] = $user[$i] -replace "([A-Z])'([A-Z])", "`$1`''`$2"

Or try one of the following. This should suffice.

get-content "mfnewuser.ins" | foreach-object {
  $_ -replace "([A-Z])'([A-Z])", "`$1`''`$2"
} | set-content "mfnewuser.ins"

...

get-content "mfnewuser.ins" | foreach-object {
  $_ -replace "([a-zA-Z', ]+)'([a-zA-Z', ]+)", "`$1`''`$2"
} | set-content "mfnewuser.ins"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.