简体   繁体   English

正则表达式查找给定范围内5个连续数字的一个或多个实例

[英]RegEx To Find One or More Instances of 5 Consecutive Digits Within A Given Range

I am a newbie at RegEx, so I need some help. 我是RegEx的新手,因此需要一些帮助。 Can anyone help me parse a string and find one or more instances of numbers that range from 40000 to 99999 ? 谁能帮我解析一个字符串并找到一个或多个数字实例,这些实例的范围从4000099999 Here are some example strings: 以下是一些示例字符串:

  • Order Details, Acme, #46405,53000,86232, for 4/17 - 3 instances 订单详细信息Acme#46405,53000,86232,4/17-3 - 3 instances
  • PSA Order Detail: Hiring Practices (46445); PSA订单详细信息:雇用惯例(46445); starting 4-16-12 - 1 instance 开始4-16-12-1 - 1 instance
  • PC Pitstop 32134 Direct for 4/18/12 - 0 instances PC Pitstop 32134 Direct for 4/18/12-0 - 0 instances

You could try something like this: 您可以尝试这样的事情:

(?<!\d)[4-9]\d{4}(?!\d)

See it on regexr regexr上看到它

EDIT: here is a c# code snippet to test it: 编辑:这是一个c#代码段进行测试:

// could be whatever
var str = "50000 alpha 43 84100";
var regex = new Regex(@"(?<!\d)[4-9]\d{4}(?!\d)");
foreach (Match match in regex.Matches(str))
    Console.WriteLine(match.Value);

this outputs: 输出:

50000
84100

You should use regex 您应该使用正则表达式

(?<!\d)([4-9]\d{4})(?!\d)

Untested code: 未经测试的代码:

using System;
using System.Collections;
using System.Text.RegularExpressions;

class Program
{
  static void Main()
  {
    string input = "50000 Order Acme, #46405,53000,86232, for 4/17 60000";
    Regex t = new Regex(@"(?<!\d)([4-9]\d{4})(?!\d)", RegexOptions.Singleline) 
    MatchCollection theMatches = t.Matches(input) 
    for (int counter = 0; counter < theMatches.Count; counter++)
    {
      Console.WriteLine(theMatches[counter].Value); 
    }
  }
}

For multi-line input use RegexOptions.Multiline instead of RegexOptions.Singleline . 对于多行输入,请使用RegexOptions.Multiline而不是RegexOptions.Singleline


Note: I suggest you always test regex with match at the begining and end of string. 注意: 我建议您始终在字符串的开头和结尾测试匹配的正则表达式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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