Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

How can I get the string before the character "-" using regular expressions?

For example, I have "text-1" and I want to return "text".

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
97 views
Welcome To Ask or Share your Answers For Others

1 Answer

So I see many possibilities to achieve this.

string text = "Foobar-test";
  1. Regex Match everything till the first "-"

    Match result = Regex.Match(text, @"^.*?(?=-)");
    
    • ^ match from the start of the string
    • .*? match any character (.), zero or more times (*) but as less as possible (?)
    • (?=-) till the next character is a "-" (this is a positive look ahead)
  2. Regex Match anything that is not a "-" from the start of the string

    Match result2 = Regex.Match(text, @"^[^-]*");
    
    • [^-]* matches any character that is not a "-" zero or more times
  3. Regex Match anything that is not a "-" from the start of the string till a "-"

    Match result21 = Regex.Match(text, @"^([^-]*)-");
    

    Will only match if there is a dash in the string, but the result is then found in capture group 1.

  4. Split on "-"

    string[] result3 = text.Split('-');
    

    Result is an Array the part before the first "-" is the first item in the Array

  5. Substring till the first "-"

    string result4 = text.Substring(0, text.IndexOf("-"));
    

    Get the substring from text from the start till the first occurrence of "-" (text.IndexOf("-"))

You get then all the results (all the same) with this

Console.WriteLine(result);
Console.WriteLine(result2);
Console.WriteLine(result21.Groups[1]);
Console.WriteLine(result3[0]);
Console.WriteLine(result4);

I would prefer the first method.

You need to think also about the behavior, when there is no dash in the string. The fourth method will throw an exception in that case, because text.IndexOf("-") will be -1. Method 1 and 2.1 will return nothing and method 2 and 3 will return the complete string.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...