本文介绍了除去价格无效字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一种情况,我必须使用C#从价格字符串中删除特定字符。

I have a scenario where I have to remove certain characters from a price string using C#.

我在找一个正规的前pression要删除这些字符或什么比这更好的。

I'm looking for a regular expression to remove these characters or something better than that.

例如,如果价格字符串是

For example, if the price string is

"3,950,000 ( Ex. TAX )"

我想删除(出含税)从字符串。

基本上,我不得不从除数字,点和逗号字符串中删除的任何字符。

Basically I have to remove the any character from string except numbers, dot and comma.

推荐答案

正前pressions总是棘手得到正确的,由于输入能够如此变化很大,但我的认为的这一个包括你的需要:

Regular expressions are always tricky to get right, since the input can vary so greatly, but I think this one covers your needs:

string pattern = @"([\d]+[,.]{0,1})+";
string cleanedPrice = Regex.Match(price, pattern).Value;

说明:

(         - start matching group
[\d]+     - match any decimal digit, at least once
[,.]{0,1} - ...followed by 0 or 1 comma or dot
)         - end of group
+         - repeat at least once

这篇关于除去价格无效字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 19:06