Link to home
Start Free TrialLog in
Avatar of Skale
Skale

asked on

How to apply regex in my case in C#

Hello,

I tried to modify one ascii file with my inputs but i have to modify some lines with specific structures like below;

Structure is like below:
subvar.str (                     #SEARCH_TERM#                ) = '#VALUE#' ! Some Text

- There is no rule for white spaces around search term there can be found or not.


This is the real example:

subvar.str (                     $_partname                ) = '"XX-2000-1050"' ! $_partname, Definition


I'd like to find "$_partname" and then change it's value "XX-2000-1050". If it's fully string i have to put queto sign on my string otherwise if it's double or integer i'll not put anything begin and end of my value.

I'll appreciate if you can help about this.
Avatar of Zvonko
Zvonko
Flag of North Macedonia image

Like this:
var pattern = @"$_partname\s*\)\s*=\s*'""[\w\-]+""'";
var newPart = @"$_partname ) = '""ZZ-3000-1050""'";
var replaced = Regex.Replace(text, pattern, newPart);

Open in new window


Or do you want to let the spaces as-is?

Avatar of Skale
Skale

ASKER

Hi Zvonko,

I'd like to let space as is, at meantime i'll be test your approach in an hour i think.

ASKER CERTIFIED SOLUTION
Avatar of Zvonko
Zvonko
Flag of North Macedonia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hello Hakan, does the Expression work for you?
Avatar of Skale

ASKER

Hi Zvonko,

Thanks for your answer it worked! Thank you again.