regex to remove ordinals

后端 未结 5 1187
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-30 14:36

I need to remove ordinals via regex, but my regex skills are quite lacking. The following locates the ordinals, but includes the digit just prior in the return value. I need

相关标签:
5条回答
  • 2020-11-30 14:36

    I came across this question, because I needed to replace ordinal numbers with dot, i. e. 1., 2., 4. etc.

    Here is the solution for this problem (in PHP):

    $entry = preg_replace('/^\d+\. /', '', $entry);

    Test: https://regex101.com/r/xLB6Ov/1

    0 讨论(0)
  • 2020-11-30 14:37

    Try a negative lookbehind:

    (?<=[0-9])(?:st|nd|rd|th)
    

    assuming the dialect of regex supports it.

    0 讨论(0)
  • 2020-11-30 14:46

    You need to use a look-behind assertion so that only st|nd|rd|th preceded by a [0-9] are matched, but the [0-9] isn't included in the match. i.e.:

    (?<=[0-9])(?:st|nd|rd|th)
    

    I've linked to the perl-compatible syntax, but if you're using posix, posix extended, vi or one of many other regex syntaxes you'll need to look up the syntax.

    0 讨论(0)
  • 2020-11-30 14:49

    If you want to remove as well the numbers followed by ordinals you could use this one:

    [0-9]+(?:st| st|nd| nd|rd| rd|th| th)
    

    So for a given text: "The 3rd person is missing but the 2 nd and the 1st is here" you'll have this output: "The person is missing but the and the is here"

    0 讨论(0)
  • 2020-11-30 14:57

    In perl:

    $var =~ s{\b(\d+)(?:st|nd|rd|th)\b}{$1};
    

    In PHP:

    $var = preg_replace('/\\b(\d+)(?:st|nd|rd|th)\\b/', '$1', $var);
    

    In .NET:

    var = Regex.Replace(@"\b(\d+)(?:st|nd|rd|th)\b", "$1");
    
    0 讨论(0)
提交回复
热议问题