官术网_书友最值得收藏!

Escaping special regex metacharacters and escaping rules inside the character classes

We know that . matches any character, [ and ] are used for character classes, { and } are used for limiting quantifiers, and ? , *, and + are used for various quantifiers. To match any of the metacharacters literally, one needs to escape these characters using a backslash (\ ) to suppress their special meaning. Similarly, ^ and $ are anchors that are also considered regex metacharacters.

Let's see some examples of escaping metacharacters in regular expressions.

The following regex matches the string, a.b?:

     a\.b\? 

The following regex matches the string, {food}:

    \{food\} 

The following regex matches the string, abc:][}{:

    abc:\]\[\}\{ 

The following regex matches the string, $25.50:

    \$\d+\.\d+

The following regex matches the string, ^*+.:

    \^\*\+\. 
主站蜘蛛池模板: 巩义市| 温泉县| 新巴尔虎左旗| 南京市| 上栗县| 余姚市| 竹北市| 秭归县| 门头沟区| 晴隆县| 北流市| 东乌珠穆沁旗| 兴化市| 金阳县| 大洼县| 东阿县| 互助| 睢宁县| 五大连池市| 宾阳县| 赤城县| 丰台区| 普陀区| 化德县| 汕头市| 宣威市| 于田县| 福建省| 保靖县| 阳新县| 重庆市| 扎囊县| 三亚市| 庐江县| 黄骅市| 敖汉旗| 威宁| 兴海县| 会泽县| 宜阳县| 林口县|