Regex/code for removing “FWD”, “RE”, etc, from email subject

后端 未结 3 1557
时光说笑
时光说笑 2021-02-07 14:35

Given an email subject line, I\'d like to clean it up, getting rid of the \"Re:\", \"Fwd\", and other junk. So, for example, \"[Fwd] Re: Jack and Jill\'s Wedding\" should turn i

3条回答
  •  一向
    一向 (楼主)
    2021-02-07 14:55

    Try this one (replace with ''):

    /([\[\(] *)?(RE|FWD?) *([-:;)\]][ :;\])-]*|$)|\]+ *$/igm
    

    (If you put each subject through as its own string then you don't need the m modifier; this is just so that $ matches end of line, not just end of string, for multiline string inputs).

    See it in action here.

    Explanation of regex:

    ([\[\(] *)?            # starting [ or (, followed by optional spaces
    (RE|FWD?) *            # RE or FW or FWD, followed by optional spaces
    ([-:;)\]][ :;\])-]*|$) # only count it as a Re or FWD if it is followed by 
                           # : or - or ; or ] or ) or end of line
                           # (and after that you can have more of these symbols with
                           #  spaces in between)
    |                      # OR
    \]+ *$                 # match any trailing \] at end of line 
                           # (we assume the brackets () occur around a whole Re/Fwd
                           #  but the square brackets [] occur around the whole 
                           #  subject line)
    

    Flags.

    i: case insensitive.

    g: global match (match all the Re/Fwd you can find).

    m: let the '$' in the regex match end of line for a multiline input, not just end of string (only relevant if you feed in all your input subjects to the regex at once. If you feed in one subject each time then you can remove it because end of line is end of string).

提交回复
热议问题