简体   繁体   中英

C# Regex to match single number among multiple numbers in a string

What regex for C# can I use that matches the a "string + some number + string + some number +string"

Sample Inputs:

Book a hotel room for 10 people  -- o/p: 10
Book a hotel room for 15 people at 10AM -- o/p: 15
Book a hotel room for 5 employees for 12 dec at 10 am -- o/p: 5
Book a hotel room in Singapore for 10 people at today -- o/p: 10
Book a hotel room for  12 dec for 10 members -- o/p: 10 

So have to fetch how many members/people/employees for booking hotel.

Hope this makes sense

A regular expression that I could plug into C# would be fantastic

I tried below pattern but not matching.

[A-Za-z]*\d+\s?(people)|(memebers)|(peoples)|(member)*$

If your number always precedes the keyword, you might not need a regex.

Try the below code.

var parts = line.Split(' ', StringSplitOptions.RemoveEmptyEntries);
var index = Array.Find(parts, p => p == "member" || p == "members" || p == "people");
int count = -1;
var found = index > 0 && int.TryParse(parts[index-1], out count);

If found is true, it indicates count has a valid value which you can use later on.

Try following :

            string[] inputs = {
                                 "Book a hotel room for 10 people  -- o/p: 10",
                                 "Book a hotel room for 15 people at 10AM -- o/p: 15",
                                 "Book a hotel room for 5 employees for 12 dec at 10 am -- o/p: 5",
                                 "Book a hotel room in Singapore for 10 people at today -- o/p: 10",
                                 "Book a hotel room for  12 dec for 10 members -- o/p: 10"
                              };

            string pattern = @"for\s+(?'count'\d+)\s+(?'type'[^\s]+)";

            foreach(string input in inputs)
            {
                MatchCollection matches = Regex.Matches(input, pattern);
                foreach (Match match in matches.Cast<Match>().AsEnumerable())
                {
                    Console.WriteLine("Count : '{0}', Type : '{1}'", match.Groups["count"].Value, match.Groups["type"].Value);
                }
            }
            Console.ReadLine();

Using the asterix * after the group (member)* will repeat the group 0 or more times so you could omit that.

Using the $ after member (member)$ will only match it at the end of the string.

You could use an alternation to match either people, member with an optional s or employee with an optional s

If you want to capture the digits as well for further processing you could also use a capturing group for that part.

\b[A-Za-z]*(\d+)\s?(people|members?|employees?)\b

Regex demo | C# demo

在此处输入图片说明

For example

string pattern = @"\b[A-Za-z]*(\d+)\s?(people|members?|employees?)\b";
string input = @"Book a hotel room for 10 people  -- o/p: 10
Book a hotel room for 15 people at 10AM -- o/p: 15
Book a hotel room for 5 employees for 12 dec at 10 am -- o/p: 5
Book a hotel room in Singapore for 10 people at today -- o/p: 10
Book a hotel room for  12 dec for 10 member -- o/p: 10 ";

foreach (Match m in Regex.Matches(input, pattern))
{
    Console.WriteLine("Match: {0}\nGroup 1: {1}\nGroup: {2}", m.Value, m.Groups[1].Value, m.Groups[2].Value);
}

If all the matches are preceded by for you might also use

\bfor (\d+)\s?(people|members?|employees?)\b

如果你只想要数字,而不是捕捉其他很多东西,也许你正在寻找这样的东西

(?<=for)(?: +)(?<number>\d+)(?= +(?:people|employee|member)s?)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM