Home > OS >  Which pattern and substitution for Regex.Replace can replace all space present inside <>?
Which pattern and substitution for Regex.Replace can replace all space present inside <>?

Time:06-21

I have data in a non-standard format which I am trying to convert to intelligible json.

Format is like

dataheads@first{
       first_data = <value1@123 value2_456 value3_789>;
       second_data = <<value4_abc_123 value5_ty>>;};

I need in this step:

first_data = <value1@123,value2_456,value3_789>;
second_data = <<value4_abc_123,value5_ty>>;

I tried Regex.Replace(contents, @"(<.*)\ (.*>)", "$1,$2"); but it only works for a single space between <>. \S*(\s)\S* messes up data outside <>. I am not sure why <\S*(\s)\S*> doesn't work. As can be seen, there are a lot more substitutions needed to convert to json so have to be careful not to mess the outsides.

CodePudding user response:

You can use this regex:

@"((?<=<[^>]*)[ ](?=[^>]*>)) "

Explanation:

( - start group

(?<=<[^>]*) - look behind for < followed by zero or more characters not being >

[ ] - match a space

(?=[^>]*>) -look ahead for zero or more characters not being > followed by >

) - repeat this group

Simply replace with ,.

Update:

You can secure the matches further, so it requires the hardcoded values before the space, by including them in the look ahead, like this:

@"((?<=<[^>]*(@123|_456|_789|_abc_123|_ty))[ ](?=[^>]*>)) ");
  • Related