简体   繁体   中英

Regular expression - text, text, number

How do you express a regular expression where the string matches the following.

text, text, number

NOTE:

text = can be any amount of words or spaces.

number = most be 4 digit number.

the commas (,) must be matched too.

As an example, the following string is valid:

'Arnold Zend, Red House, 2551'

The regex pattern for that would be (the parenthesis are capture groups in-case you want to access the individual items:

([a-zA-Z\s]{3,}), ([a-zA-Z\s]*{3,}), ([0-9]{4})

It matches 2 names and a 4 digit number separated by a comma with the names being at-least 3 characters long. You can change the name character minimum if you'd like. And this is how to check if a string matches this pattern:

// 'Regex' is in the System.Text.RegularExpressions namespace.

Regex MyPattern = new Regex(@"([a-zA-Z\s]*), ([a-zA-Z\s]*), ([0-9]{4})");

if (MyPattern.IsMatch("Arnold Zend, Red House, 2551")) {
    Console.WriteLine("String matched.");
}

I have tested the expression with RegexTester and it works fine.

I would use the regular expression:

(?<Field1>[\\w\\s]+)\\s*,\\s*(?<Field2>[\\w\\s]+)\\s*,\\s*(?<Number>\\d{4})

\\w = All letters (upper and lower case) and underscores. + indicates one or more .

\\s = Whitespace characters. * indicates zero or more .

\\d = Digits 0 through 9. {4} indicates it must be four exactly.

(?<Name>) = Capture group name and pattern to match.

You can use this with the Regex object in the System.Text.RegularExpressions namespace, like so:

  static readonly Regex lineRegex = new Regex(@"(?<Field1>[\w\s]+)\s*,\s*(?<Field2>[\w\s]+)\s*,\s*(?<Number>\d{4})");

  // You should define your own class which has these fields and out
  // that as a single object instead of these three separate fields.

  public static bool TryParse(string line, out string field1,
                                           out string field2, 
                                           out int number)
  {
    field1 = null;
    field2 = null;
    number = 0;

    var match = lineRegex.Match(line);

    // Does not match the pattern, cannot parse.
    if (!match.Success) return false;

    field1 = match.Groups["Field1"].Value;
    field2 = match.Groups["Field2"].Value;

    // Try to parse the integer value.
    if (!int.TryParse(match.Groups["Number"].Value, out number))
      return false;

    return true;
  }

尝试这个 -

[\w ]+, [\w ]+, \d{4}

([[a-zA-Z \\ s] +),([a-zA-Z \\ s] +),([0-9] {4})

要与unicode兼容:

^[\pL\s]+,[\pL\s]+,\pN+$

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