0

This is rather a RegEx question than PS one. Here it goes:

I have a text file with data like below.

ABC Corp, x567 
xyz Corp, y567 
pqr Corp, m567 
ghysds ,inc, x567 
TRWsdsdsds ,org, y567 
TYUds ,ing, m567

How can I remove the first comma from line 4-6? (These lines have 2 commas. I need only the second one.) My plan is to insert this data into a table with 2 columns.

Thank you.

1
  • you could try this link: The second Answer is what you want. Commented Mar 14, 2014 at 1:02

3 Answers 3

2

You have to use look ahead to check to see if there is a second comma on the line.

,(?=.*,)

Use this to replace whatever it matches with an empty string. This will get rid of the first comma of lines that have two commas in them.

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

2 Comments

This is the most elegant way.
Thank you Kabb! Very nice answer. Unfortunately, being a newbie, I cannot promote your answer.
1

Here's mine:

$text = 
(@'
ABC Corp, x567
xyz Corp, y567 
pqr Corp, m567 
ghysds ,inc, x567 
TRWsdsdsds ,org, y567 
TYUds ,ing, m567
'@).split("`n")

$text -replace '(.+?),(.+?),(.+)','$1$2,$3'

ABC Corp, x567
xyz Corp, y567 
pqr Corp, m567 
ghysds inc, x567 
TRWsdsdsds org, y567 
TYUds ing, m567

Comments

0

You don't really need regular expressions for this, although it would work.

$StringList = @('abd,asdc,asdc', 'asdc,awegwe,aweg', 'asdfasdf,asdaweg');

foreach ($String in $StringList) {
    if ($String -match '.*,.*,') {
        $String.Remove($String.IndexOf(','), 1);
    }
    else {
        $String;
    }
}

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.