Regex: ignore case sensitivity

后端 未结 13 1819
孤城傲影
孤城傲影 2020-11-22 06:11

How can I make the following regex ignore case sensitivity? It should match all the correct characters but ignore whether they are lower or uppercase.

G[a-b]         


        
相关标签:
13条回答
  • 2020-11-22 06:54

    Depends on implementation but I would use

    (?i)G[a-b].
    

    VARIATIONS:

    (?i) case-insensitive mode ON    
    (?-i) case-insensitive mode OFF
    

    Modern regex flavors allow you to apply modifiers to only part of the regular expression. If you insert the modifier (?im) in the middle of the regex then the modifier only applies to the part of the regex to the right of the modifier. With these flavors, you can turn off modes by preceding them with a minus sign (?-i).

    Description is from the page: https://www.regular-expressions.info/modifiers.html

    0 讨论(0)
  • 2020-11-22 06:55

    You also can lead your initial string, which you are going to check for pattern matching, to lower case. And using in your pattern lower case symbols respectively .

    0 讨论(0)
  • 2020-11-22 06:56

    Addition to the already-accepted answers:

    Grep usage:

    Note that for greping it is simply the addition of the -i modifier. Ex: grep -rni regular_expression to search for this 'regular_expression' 'r'ecursively, case 'i'nsensitive, showing line 'n'umbers in the result.

    Also, here's a great tool for verifying regular expressions: https://regex101.com/

    Ex: See the expression and Explanation in this image.

    References:

    • man pages (man grep)
    • http://droptips.com/using-grep-and-ignoring-case-case-insensitive-grep
    0 讨论(0)
  • 2020-11-22 06:57

    [gG][aAbB].* probably simples solution if the pattern is not too complicated or long.

    0 讨论(0)
  • 2020-11-22 06:58

    C#

    using System.Text.RegularExpressions;
    ...    
    Regex.Match(
        input: "Check This String",
        pattern: "Regex Pattern",
        options: RegexOptions.IgnoreCase)
    

    specifically: options: RegexOptions.IgnoreCase

    0 讨论(0)
  • 2020-11-22 07:00

    The i flag is normally used for case insensitivity. You don't give a language here, but it'll probably be something like /G[ab].*/i or /(?i)G[ab].*/.

    0 讨论(0)
提交回复
热议问题