c# - Replace specific capture group values if its value equals zero -
is possible replace specific capture group values value if value equals 0 (zero) in c#?
match match = regex.match(input, "\b([1-9])([0-9])([0-9]).([0-9])([0-9])([0-9])\b"); if (match.success) { if(match.groups[5].value == "0" || match.groups[6].value == "0") { } }
basically have input mhz frequency, accepts values form 100.000-199.999. want if last 2 digits zeros, want remove them. example, if input 197.900, want replace 197.9. if it's 197.990 => 197.99. if 197.090 => 197.09...so on forth.
additionally, after removing digits, want rewrite string xxx point xxx...
regex.replace(input, "\b([1-9])([0-9])([0-9]).([0-9])([0-9])([0-9])\b", "$1 $2 $3 point $4 $5 $6");
i'm not sure how go doing this.
to truncate zeroes:
string text = "frequencies 193.200 , 194.220 mhz. 3 digits used: 195.123"; string output = regex.replace(text, @"\b[1-9]\d{2}\.\d{3}\b", m => m.value.trimend('0'));
output:
frequencies 193.2 , 194.22 mhz. 3 digits used: 195.123
to truncate zeroes , replace .
point
.
string text = "frequencies 193.200 , 194.220 mhz. 3 digits used: 195.123"; string output = regex.replace(text, @"\b([1-9]\d{2})\.(\d{3})\b", m => string.format("{0} point {1}", m.groups[1].value, m.groups[2].value.trimend('0')));
output:
frequencies 193 point 2 , 194 point 22 mhz. 3 digits used: 195 point 123
to deal numbers having 0
decimal part, ex.: 196.000
can use following code:
string text = "193.200, 194.220, 195.123, 196.000."; string output = regex.replace(text, @"\b([1-9]\d{2})\.(\d)(\d{2})\b", m => string.format("{0} point {1}{2}", m.groups[1].value, m.groups[2].value, m.groups[3].value.trimend('0')));
output:
193 point 2, 194 point 22, 195 point 123, 196 point 0.
Comments
Post a Comment