RegEx.Replace match optional

  • Thread starter Thread starter kids_pro
  • Start date Start date
K

kids_pro

Hi there,

Could someone help me out from this situation?
I want to write a pattern to search and replace.

string pat = "e([\u1780-\u17A2]\u17D2[\u1780-\u17A2])a";
string result = Regex.Replace(instr, pat, "$1\u17CE");

What I want is to make this part \u17D2[\u1780-\u17A2] as optional.
I have tried it this way: string pat =
"e([\u1780-\u17A2]\u17D2?[\u1780-\u17A2]?)a";

but it seem not working.

Please advice.
Regards,
Kids
 
Tough to comment on without seeing examples of the string you're trying to
match, but here goes:

Group the optional expression: \u17D2[\u1780-\u17A2] in parens and follow
with a question mark thus: (\u17D2[\u1780-\u17A2])?

In my experience, an overly-repetitive regex sometimes indicates a problem
in its design.

Go back and try a simpler version of it and then build upon it, for example:

e[\u1780-\u17A2]*a

Post some examples of the strings you wish to match (at least a few of the
most diverse ones).

--
Alex T. Silverstein
Systems Architect, Owner
The Unified Digital Group, LLC
Complete IT Consulting for Small Business
Website: http://www.unifieddigital.com
 
Thank Alex,

I got it work with many try.
I think it might related to the unicode character order.

with the sample string like this: e<unicode><coeng_unicode><uni_code>a
"e([\u1780-\u17A2](\u17D2)?)a"
the result now I can convert the e and a to \u17C4.

Thank for the clue.
Regards,
Kids



Alex T. Silverstein said:
Tough to comment on without seeing examples of the string you're trying to
match, but here goes:

Group the optional expression: \u17D2[\u1780-\u17A2] in parens and follow
with a question mark thus: (\u17D2[\u1780-\u17A2])?

In my experience, an overly-repetitive regex sometimes indicates a problem
in its design.

Go back and try a simpler version of it and then build upon it, for example:

e[\u1780-\u17A2]*a

Post some examples of the strings you wish to match (at least a few of the
most diverse ones).

--
Alex T. Silverstein
Systems Architect, Owner
The Unified Digital Group, LLC
Complete IT Consulting for Small Business
Website: http://www.unifieddigital.com

kids_pro said:
Hi there,

Could someone help me out from this situation?
I want to write a pattern to search and replace.

string pat = "e([\u1780-\u17A2]\u17D2[\u1780-\u17A2])a";
string result = Regex.Replace(instr, pat, "$1\u17CE");

What I want is to make this part \u17D2[\u1780-\u17A2] as optional.
I have tried it this way: string pat =
"e([\u1780-\u17A2]\u17D2?[\u1780-\u17A2]?)a";

but it seem not working.

Please advice.
Regards,
Kids
 
Back
Top