获取字符串C#中第一个非Alpha字符的索引

时间:2015-10-22 17:32:08

标签: c# regex

每当我找到第一个非alpha或空格时,我都试图分割出一个字符串(在索引处)。

我的正则表达式真的很生疏,并试图找到一些方向让这个工作。

示例:" 2015年8月14日收到的付款$ 0.00"是字符串。我能找到第一个数字

data:
[(7.0, 2025.0, 0.14473006433340618), (7.0, 2030.0, 0.14062607337060892), (7.0, 2020.0, 0.1472362005164195), (7.0, 2040.0, 0.1319299850069502), (7.0, 2050.0, 0.12497732155598347), (38.0, 2040.0, 0.13013012553239645), (38.0, 2050.0, 0.12603323962393054), (38.0, 2020.0, 0.13923258922827567), (38.0, 2025.0, 0.1371105447700807), (38.0, 2030.0, 0.13509217383084055), (7.0, 2035.0, 0.13590798149865668), (7.0, 2055.0, 0.12122832793319385), (38.0, 2045.0, 0.1283033928063951), (38.0, 2055.0, 0.12376727418426803), (7.0, 2045.0, 0.12747859242231532), (38.0, 2035.0, 0.13268757208364956), (28.0, 2040.0, 0.1336849736130551), (28.0, 2045.0, 0.1304881237751081), (28.0, 2050.0, 0.12838196945626767), (28.0, 2055.0, 0.12594690413524617), (28.0, 2020.0, 0.1426266123329183), (28.0, 2025.0, 0.14105151195673143), (28.0, 2030.0, 0.1373925658286551), (28.0, 2035.0, 0.13591663964396278), (0.0, 2040.0, 0.13520161504277875), (0.0, 2045.0, 0.12954944890882458), (0.0, 2050.0, 0.12265378802459166), (0.0, 2055.0, 0.11829387116926045), (0.0, 2020.0, 0.15941121559558222), (0.0, 2025.0, 0.15264786078400508), (0.0, 2030.0, 0.14752256519025417), (0.0, 2035.0, 0.1410671600261568), (14.0, 2055.0, 0.12290037442735217), (14.0, 2050.0, 0.12609729724766153), (14.0, 2045.0, 0.12957885633601618), (14.0, 2040.0, 0.13191236031636167), (14.0, 2035.0, 0.13486885042437866), (14.0, 2030.0, 0.13735387420193756), (14.0, 2025.0, 0.14117139286403094), (14.0, 2020.0, 0.14410654811736545), (21.0, 2040.0, 0), (21.0, 2045.0, 0.1280055085205311), (21.0, 2050.0, 0.1253472936767727), (21.0, 2055.0, 0.12311151990554384), (21.0, 2020.0, 0.14150097628342895), (21.0, 2025.0, 0.13862160845657018), (21.0, 2030.0, 0.1357506086172204), (21.0, 2035.0, 0.1334635475535782), (35.0, 2040.0, 0), (35.0, 2045.0, 0.12909917255032188), (35.0, 2050.0, 0.12671861528494266), (35.0, 2055.0, 0.12432370882456634), (35.0, 2020.0, 0.14029296457584772), (35.0, 2025.0, 0.13813227231612185), (35.0, 2030.0, 0.13606666884642726), (35.0, 2035.0, 0)]
shape:
[(7.0, 2025.0, 1162), (7.0, 2030.0, 381), (7.0, 2020.0, 944), (7.0, 2040.0, 3074), (7.0, 2050.0, 2141), (38.0, 2040.0, 514), (38.0, 2050.0, 393), (38.0, 2020.0, 15), (38.0, 2025.0, 7), (38.0, 2030.0, 30), (7.0, 2035.0, 63), (7.0, 2055.0, 1676), (38.0, 2045.0, 123), (38.0, 2055.0, 2), (7.0, 2045.0, 3924), (38.0, 2035.0, 23), (28.0, 2040.0, 7238), (28.0, 2045.0, 3908), (28.0, 2050.0, 16383), (28.0, 2055.0, 2835), (28.0, 2020.0, 2058), (28.0, 2025.0, 1481), (28.0, 2030.0, 831), (28.0, 2035.0, 1386), (0.0, 2040.0, 9437), (0.0, 2045.0, 0), (0.0, 2050.0, 0), (0.0, 2055.0, 0), (0.0, 2020.0, 0), (0.0, 2025.0, 901), (0.0, 2030.0, 0), (0.0, 2035.0, 0), (14.0, 2055.0, 384), (14.0, 2050.0, 1482), (14.0, 2045.0, 928), (14.0, 2040.0, 800), (14.0, 2035.0, 0), (14.0, 2030.0, 21), (14.0, 2025.0, 124), (14.0, 2020.0, 400), (21.0, 2040.0, 598), (21.0, 2045.0, 385), (21.0, 2050.0, 397), (21.0, 2055.0, 817), (21.0, 2020.0, 40), (21.0, 2025.0, 13), (21.0, 2030.0, 26), (21.0, 2035.0, 24), (35.0, 2040.0, 141), (35.0, 2045.0, 365), (35.0, 2050.0, 324), (35.0, 2055.0, 49), (35.0, 2020.0, 294), (35.0, 2025.0, 422), (35.0, 2030.0, 112), (35.0, 2035.0, 22)]

问题出在一个字符串,如" Amount This Period + $ 57.00" 我结束了"金额这个时期+ $"

如何在C#中使用Regex,如果我还想包含特定的非字母数字字符来检查string alphabet = String.Empty; string digit = String.Empty; int digitStartIndex; Match regexMatch = Regex.Match("Payments Received by 08/14/2015 $0.00", "\\d"); digitStartIndex = regexMatch.Index; alphabet = line.Substring(0, digitStartIndex); digit = line.Substring(digitStartIndex);

修改:我正在寻找上面例子中的输出(变量字母和数字)我正在努力做到。
"此期间金额"
" + $ 57.00"

2 个答案:

答案 0 :(得分:2)

啊,那么你想要这个我想:

void Main()
{
    var regex = new Regex(@"(.*?)([\$\+\-].*)");
    var a = "Payments Received by 08/14/2015 $0.00";
    var b = "Amount This Period + $57.00";

    Console.WriteLine(regex.Match(a).Groups[1].Value);
    Console.WriteLine(regex.Match(a).Groups[2].Value);
    Console.WriteLine(regex.Match(b).Groups[1].Value);
    Console.WriteLine(regex.Match(b).Groups[2].Value);
}

输出:

Payments Received by 08/14/2015 
$0.00
Amount This Period 
+ $57.00

答案 1 :(得分:2)

要按照您提到的方式拆分字符串,请使用正则表达式查找初始字母/空格字符,然后使用其余字符。

var s = "Payments Received by 08/14/2015 $0.00";
var re = new Regex("^([a-z ]+)(.+)", RegexOptions.IgnoreCase);
var m = re.Match(s);
if (m.Success)
{
    Console.WriteLine(m.Groups[1]);
    Console.WriteLine(m.Groups[2]);
}

^在开始时找到字符非常重要。