Why can't Regular Expressions use keywords instead of characters?

后端 未结 14 3179
甜味超标
甜味超标 2021-02-20 06:46

Okay, I barely understand RegEx basics, but why couldn\'t they design it to use keywords (like SQL) instead of some cryptic wildcard characters and symbols?

Is it for pe

14条回答
  •  Happy的楠姐
    2021-02-20 07:18

    Well, if you had keywords, how would you easily differentiate them from actually matched text? How would you handle whitespace?

    Source text Company: A Dept.: B

    Standard regex:

    Company:\s+(.+)\s+Dept.:\s+(.+)
    

    Or even:

    Company: (.+) Dept. (.+)
    

    Keyword regex (trying really hard not get a strawman...)

    "Company:" whitespace.oneplus group(any.oneplus) whitespace.oneplus "Dept.:" whitespace.oneplus group(any.oneplus)
    

    Or simplified:

    "Company:" space group(any.oneplus) space "Dept.:" space group(any.oneplus)
    

    No, it's probably not better.

提交回复
热议问题