English 中文(简体)
如何更简明扼要地撰写这些定期表述?
原标题:How can these regular expressions be written more concisely?
  • 时间:2012-04-13 23:13:55
  •  标签:
  • regex

我能否将这些定期表达结合起来?

        _regexes = new List<Regex>();
        _regexes.Add(new Regex("[0-9]*th Fl$"));
        _regexes.Add(new Regex("[0-9]*th Floor$"));
        _regexes.Add(new Regex("[0-9]*st Fl$"));
        _regexes.Add(new Regex("[0-9]*st Floor$"));
        _regexes.Add(new Regex("[0-9]*nd Fl$"));
        _regexes.Add(new Regex("[0-9]*nd Floor$"));
        _regexes.Add(new Regex("[0-9]*rd Flr$"));
        _regexes.Add(new Regex("[0-9]*rd Floor$"));
最佳回答
d+(?:st|th|nd|rd) Fl(?:r|oor)?$
  • Match at least a digit or more, not zero digits (which is probably a shortcoming in the original)
  • Don t forget st
  • Match Fl, Floor or Flr
  • Use non-capturing group (?: instead of capturing group
  • Assumes perl compatible regex
问题回答
[0-9]*(st|th|nd|rd) (Fl|Floor)$ 

发挥同样的作用。 您使用/code>的操作员,实际上要说一种可能性。

[假定_regexes.Add(new Regex ("[0-9]*rd Flr$”); - the r is a打字]?





相关问题
Uncommon regular expressions [closed]

Recently I discovered two amazing regular expression features: ?: and ?!. I was curious of other neat regex features. So maybe you would like to share some tricky regular expressions.

regex to trap img tag, both versions

I need to remove image tags from text, so both versions of the tag: <img src="" ... ></img> <img src="" ... />

C++, Boost regex, replace value function of matched value?

Specifically, I have an array of strings called val, and want to replace all instances of "%{n}%" in the input with val[n]. More generally, I want the replace value to be a function of the match ...

PowerShell -match operator and multiple groups

I have the following log entry that I am processing in PowerShell I m trying to extract all the activity names and durations using the -match operator but I am only getting one match group back. I m ...

Is it possible to negate a regular expression search?

I m building a lexical analysis engine in c#. For the most part it is done and works quite well. One of the features of my lexer is that it allows any user to input their own regular expressions. This ...

regex for four-digit numbers (or "default")

I need a regex for four-digit numbers separated by comma ("default" can also be a value). Examples: 6755 3452,8767,9865,8766,3454 7678,9876 1234,9867,6876,9865 default Note: "default" ...