在C#中使用固定模式从字符串中提取特定数字

时间:2016-02-18 10:47:26

标签: c# string

这听起来像是一个非常基本的问题,但是C#给我带来了很多麻烦。

假设我有以下String被称为我的chosenTarget.title s:

2008/SD128934 - Wordz aaaaand more words (1233-26-21)
20998/AD1234 - Wordz and less words (1263-21-21)
208/ASD12345 - Wordz and more words (1833-21-21)

现在你可以看到,所有三个String在某些方面都有所不同。

我需要的是提取这些String的一个非常具体的部分,但正确地解释这些微妙之处让我感到困惑,我想知道你们中有些人是否比我更了解。

我所知道的是String将始终采用以下模式:

yearNumber + "/" + aFewLetters + theDesiredNumber + " - " + descriptiveText + " (" + someDate + ")"

在上面的例子中,我想要回到我身边的是:

128934
1234
12345

我需要提取theDesiredNumber

现在,我不是那么懒,所以我自己做了几次尝试:

var a = chosenTarget.title.Substring(chosenTarget.title.IndexOf("/") + 1, chosenTarget.title.Length - chosenTarget.title.IndexOf("/"));

这样做是切出yearNumber/,在aFewLetter之前留下theDesiredNumber

我很难正确地删除其余部分,我想知道你们中是否有人可以帮我解决这个问题?

6 个答案:

答案 0 :(得分:3)

听起来好像你只需要提取以/结尾的第一个-后面的数字。您可以使用字符串方法和LINQ的组合:

int startIndex = str.IndexOf("/");
string number = null;
if (startIndex >= 0 )
{
    int endIndex = str.IndexOf(" - ", startIndex);
    if (endIndex >= 0)
    {
        startIndex++;
        string token = str.Substring(startIndex, endIndex - startIndex); // SD128934
        number = String.Concat(token.Where(char.IsDigit)); // 128934
    }
}

使用String.Split的另一种主要LINQ方法:

number = String.Concat(
            str.Split(new[] { " - " }, StringSplitOptions.None)[0]
              .Split('/')
              .Last()
              .Where(char.IsDigit));

答案 1 :(得分:1)

试试这个:

 int indexSlash = chosenTarget.title.IndexOf("/");
 int indexDash = chosenTarget.title.IndexOf("-");
 string out = new string(chosenTarget.title.Substring(indexSlash,indexDash-indexSlash).Where(c => Char.IsDigit(c)).ToArray());

答案 2 :(得分:1)

您可以使用正则表达式:

@Autowired
internal var mongoTemplate: MongoTemplate

@Autowired
internal var solrClient: SolrClient

或者你可以循环每一行并使用Match而不是Matches。在这种情况下,您不需要构建一个"匹配器"在每次迭代中,但在循环之外构建它

答案 3 :(得分:1)

正则表达式是你的朋友:

(new [] {"2008/SD128934 - Wordz aaaaand more words (1233-26-21)",
"20998/AD1234 - Wordz and less words (1263-21-21)",
"208/ASD12345 - Wordz and more words (1833-21-21)"})
.Select(x => new Regex(@"\d+/[A-Z]+(\d+)").Match(x).Groups[1].Value)

答案 4 :(得分:1)

您认识到的模式非常重要,这是解决方案:

const string pattern = @"\d+\/[a-zA-Z]+(\d+).*$";
string s1 = @"2008/SD128934 - Wordz aaaaand more words(1233-26-21)";
string s2 = @"20998/AD1234 - Wordz and less words(1263-21-21)";
string s3 = @"208/ASD12345 - Wordz and more words(1833-21-21)";
var strings = new List<string> { s1, s2, s3 };
var desiredNumber = string.Empty;

foreach (var s in strings)
{
    var match = Regex.Match(s, pattern);
    if (match.Success)
    {
        desiredNumber = match.Groups[1].Value;
    }
}

答案 5 :(得分:1)

我会为此使用RegEx,您正在寻找的字符串位于Match.Groups [1]

        string composite = "2008/SD128934 - Wordz aaaaand more words (1233-26-21)";
        Match m= Regex.Match(composite,@"^\d{4}\/[a-zA-Z]+(\d+)");
        if (m.Success) Console.WriteLine(m.Groups[1]);

RegEx的细分如下

"^\d{4}\/[a-zA-Z]+(\d+)"

^           - Indicates that it's the beginning of the string
\d{4}       - Four digits
\/          - /
[a-zA-Z]+   - More than one letters
(\d+)       - More than one digits (the parenthesis indicate that this part is captured as a group - in this case group 1)