Perl Regular expression | how to exclude words from a file

后端 未结 5 1630
夕颜
夕颜 2021-01-21 17:47

i searching to find some Perl Regular Expression Syntax about some requirements i have in a project. First i want to exclude strings from a txt file (dictionary).

For ex

5条回答
  •  一整个雨季
    2021-01-21 18:05

    To not match a word from a file you might check whether a string contains a substring or use a negative lookahead and an alternation:

    ^(?!.*(?:tree|car|ship)).*$
    
    • ^ Assert start of string
    • (?! negative lookahead, assert what is on the right is not
      • .*(?:tree|car|ship) Match 0+ times any char except a newline and match either tree car or ship
    • ) Close negative lookahead
    • .* Match any char except a newline
    • $ Assert end of string

    Regex demo

    To not allow a string to have over 3 times a char repeat you could use:

    \b(?!(?:\w*(\w)\1){3})\w+\b
    
    • \b Word boundary
    • (?! Negative lookahead, assert what is on the right is not
      • (?: NOn capturing group
      • \w*(\w)\1 Match 0+ times a word character followed by capturing a word char in a group followed by a backreference using \1 to that group
      • ){3} Close non capturing group and repeat 3 times
    • ) close negative lookahead
    • \w+ Match 1+ word characters
    • \b word boundary

    Regex demo

    Update

    According to this posted answer (which you might add to the question instead) you have 2 patterns that you want to combine but it does not work:

    (?=^(?!(?:\w*(.)\1){3}).+$)(?=^(?:(.)(?!(?:.*?\1){4}))*$)
    

    In those 2 patterns you use 2 capturing groups, so the second pattern has to point to the second capturing group \2.

    (?=^(?!(?:\w*(.)\1){3}).+$)(?=^(?:(.)(?!(?:.*?\2){4}))*$)
                                                   ^  
    

    Pattern demo

提交回复
热议问题