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

后端 未结 14 3178
甜味超标
甜味超标 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条回答
  •  故里飘歌
    2021-02-20 07:11

    Regular expressions have a mathematical (actually, language theory) background and are coded somewhat like a mathematical formula. You can define them by a set of rules, for example

    • every character is a regular expression, representing itself
    • if a and b are regular expressions, then a?, a|b and ab are regular expressions, too
    • ...

    Using a keyword-based language would be a great burden for simple regular expressions. Most of the time, you will just use a simple text string as search pattern:

    grep -R 'main' *.c
    

    Or maybe very simple patterns:

    grep -c ':-[)(]' seidl.txt
    

    Once you get used to regular expressions, this syntax is very clear and precise. In more complicated situations you will probably use something else since a large regular expression is obviously hard to read.

提交回复
热议问题