C# 使用正则表达式将字符串数组转换为Int数组
下面是我到目前为止的代码,它不是我想要的那样工作,所以我需要一些帮助C# 使用正则表达式将字符串数组转换为Int数组,c#,regex,arrays,string,int,C#,Regex,Arrays,String,Int,下面是我到目前为止的代码,它不是我想要的那样工作,所以我需要一些帮助 字符串[]FCVs返回时带有字符串…每个字符串看起来都有点像这样$103700如果您只想返回一个int,那么可以如下所示: int outputValue; int.TryParse(Regex.Replace(inputString, @"[\D]", ""), out outputValue); 这将103700放入outputValue变量。您将需要对其进行扩展,并为没有数字的项目添加处理。我找到了答案。我非常接近。完
字符串[]
FCVs
返回时带有字符串…每个字符串看起来都有点像这样$103700如果您只想返回一个int,那么可以如下所示:
int outputValue;
int.TryParse(Regex.Replace(inputString, @"[\D]", ""), out outputValue);
这将103700放入outputValue变量。您将需要对其进行扩展,并为没有数字的项目添加处理。我找到了答案。我非常接近。完成下面的代码
public static List<object> processPage(string sourceCode)
{
//create List<object> to return
List<object> ItemsToReturn = new List<object>();
string Description = getBetween(sourceCode, @"Description:</td><td style=""padding-top: 5px; padding-bottom: 5px; font-size: 8pt; vertical-align: top;"">", "</td>");
//add description (string) to List<object>
ItemsToReturn.Add(Description);
//pull section to sort through from sourcecode
string FullCashValue = getBetween(sourceCode, @"Full Cash Value</a>", "<a href");
string[] FCVs = new string[2];
//find index of $ sign
int index1 = FullCashValue.IndexOf("$");
//find $ amount + some extra characters for wiggle room
FCVs[0] = FullCashValue.Substring(index1, 15).ToString(); //2014
int index2 = FullCashValue.IndexOf("$", index1 + 1);
FCVs[1] = FullCashValue.Substring(index2, 15).ToString(); //2013
int[] int_FCVs = new int[5];
for (int i = 0; i < FCVs.Count(); i++)
{
// replace all non-digits with ""
var m = Regex.Replace(FCVs[i], @"[^.0-9]", "");
//convert var m to Int & place into array of ints
int_FCVs[i] = Convert.ToInt32(m);
}
//put each int into ItemsToReturn (list<object>)
foreach (int FCV in int_FCVs)
{
ItemsToReturn.Add(FCV);
}
return ItemsToReturn;
}
公共静态列表进程页(字符串源代码)
{
//创建要返回的列表
List ItemsToReturn=新列表();
string Description=getBetween(源代码,@“Description:”,“”);
//将说明(字符串)添加到列表
ItemsToReturn.Add(说明);
//从源代码中拉出要排序的部分
string FullCashValue=getBetween(sourceCode,@“Full Cash Value”,“如果您根据答案和问题将HTML加载到HTMLDocument
,或者如果您知道HTML是有效的XML(不是给定的!),您可能会发现从HTML中删除各种比特和片段更简单,您可以将其加载到一个XmlDocument中。在这两种情况下,您都可以使用XPath选择器轻松提取所需内容的各个部分
但是,如果文本包含与您类似的货币值,我可能会编写一个小助手方法来从文本中提取货币值:
public static IEnumerable<string> ParseCurrencyValuesFromString( this string s )
{
for ( Match m = rxCurrencyValue.Match( s ) ; m.Success ; m.NextMatch() )
{
yield return m.Value ;
}
}
private static Regex rxCurrencyValue = new Regex( @"\$\d\d?\d?(,\d\d\d)*(\.\d+)?");
让正则表达式引擎为您完成这项工作
如果你想变得更有趣,你可以检查当前的文化,并使用定义的货币符号、千位分隔符和小数点动态构造正则表达式。如果你将有负数,则正则表达式需要更有趣一些,特别是如果它们由bein指示会计风格用括号括起来,但不多。你在问什么?你有什么问题?
public static IEnumerable<string> ParseCurrencyValuesFromString( this string s )
{
for ( Match m = rxCurrencyValue.Match( s ) ; m.Success ; m.NextMatch() )
{
yield return m.Value ;
}
}
private static Regex rxCurrencyValue = new Regex( @"\$\d\d?\d?(,\d\d\d)*(\.\d+)?");
string text = "$1, $2.34, $123, $1,234, $12,345, $123,456, $12,345,678.9012" ;
int[] intValues = text.ParseCurrencyValuesFromString()
.Select( v => decimal.Parse(v,NumberStyles.Currency))
.Select( d => (int) Math.Round(d,MidpointRounding.ToEven))
.ToArray()
;