1

I have one string like

var input =  "data1, data2, 1233456,  \"\"\" test, data, here \"\"\", 08976, test data"

I want to replace \"\"\" test, data, here \"\"\" part of this string with test; data; here

In simple words replace comma ',' with semincolon ';' any string inside \"\"\" block only.

I am trying to do this with a Regular expression.

I am trying to use following regex - \[\\\\\"](.+)[\\\\\"]

2
  • 2
    Go ahead and add your abortive code attempts to your question. It won't make the question more clear, but it may allow people to point out a small misconception you may have...and it also makes people much more likely to answer the question (odd, but true.) Commented Oct 4, 2013 at 12:38
  • 2
    As @Beska says ... people are more likely to help if they know you have tried. Posting your code shows that Commented Oct 4, 2013 at 12:41

4 Answers 4

2

Just for reference, if you want a non-regex solution to compare, you can do this with LINQ as well:

input= string.Join("\"\"\"", 
         input.Split(new []{"\"\"\""}, StringSplitOptions.None)
         .Select( (s,i) => i % 2 == 1 ? s.Replace (',', ';') : s)
       );
Sign up to request clarification or add additional context in comments.

Comments

1

Thanks guy for help,

Your answers were useful.

Finally managed to do this with following code with help of this link

//My input string
var input  = Regex.Replace(input  , "[\\\"](.+)[\\\"]", ReplaceMethod);


//Method used to replace 
public static string ReplaceMethod(Match m)
    {
        string newValue = m.Value;
        return newValue.Replace("\"", "").Replace(",", ";");
    }

Comments

0

I don't think it is possible to do this with regex for this string:
data1, 1233456, """ test, data, here """, 08976, test, """ second, data """, aso

It is possible for:
data1, 1233456, < test, data, here >, 08976, test, < second, data >, aso

but not "xxx"

pattern: \"{3}.*\"{3}
foreach regex matching this pattern string.replace(',', ';')

but i'm trying to make regex...
and I give up :/

1 Comment

Please check my answer, it's possible
0

The following code is might satisfy the requirement...

var output = Regex.Replace(input , "(?<=\"\"\".+),(?=.+\"\"\")", ";");

4 Comments

I don't think it is possible to do this with regex for this string: data1, 1233456, """ test, data, here """, 08976, test, """ second, data """, aso
Can you confirm that the input is data1, data2, 1233456, """ test, data, here """, 08976, test data (without the escape characters)?
When you run above Regex.Replace you should get...data1, data2, 1233456, """ test; data; here """, 08976, test data (without the escape). Let me know if that is/isn's what you are looking for.
you can test here: fiddle.re/5mawn but even if you fix this it will be correct for data after """ and before begining of the next """

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.