shell grep正则匹配汉字

 Shell grep正则匹配中文

测试文本 demo_exe.c,内容如下,需要注意保存的编码格式,对输出到终端有影响:

我们中文操作系统ASNI默认是GBK的。

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #include <string.h>
 4 #include <errno.h>
 5 #include <locale.h>
 6 #include <dlfcn.h>
 7 
 8 /*
 9  * export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/data/local/tmp; /data/local/tmp/demo_exe
10  */
11 int main(int argc, char** argv) {
12 // 这个是中文
13     void *handle = NULL;
14     char* locname = setlocale(LC_ALL, "");
15 // 这个是中文
16 //
17     if ((handle = dlopen(demo_dso_so, RTLD_NOW)) == NULL) {
18         printf("dlopen出错: %s\n", dlerror());
19     }
20     printf("@%s[%s]dlopen return handle = %#x.\n", __FILE__, __FUNCTION__, handle);
21 // 这个是
22 // 中文
23     return 0;
24 }

 

1、匹配特定文字:

$ grep -nP "\xE4\xB8\xAD\xE6\x96\x87|\xD6\xD0\xCE\xC4" ./demo_exe.c
12:// 这个是中文
15:// 这个是中文
22:// 中文

编码 在线码表
GBK D6D0 CEC4 http://www.lhelper.org/tech/chinese_internal_code_specification_classified.txt
Unicode 4E2D 6587  
UTF-8 %E4%B8%AD %E6%96%87 http://wenku.baidu.com/link?url=DfbzjKLcRaQ7yVIA_EHVP7mKdVbkggq4hwkCmmO9uR76Jib_5Y1Y_h616NnI21XY_x85YZqN1SQBAdCFQjklS_

 

GBK码 : 中=D6D0,文=CEC4

Unicode码:中=4E2D,文=6587

UTF-8码:中=%E4%B8%AD,文=%E6%96%87

 

2、匹配特定范围文字

$ grep -nP "[\xB0\xA1-\xF7\xFE]+" /home/fangss/c/dynamic_share_object_test/demo_exe.c
12:// 这个是中文
15:// 这个是中文
18: printf("dlopen出错: %s\n", dlerror());
21:// 这个是
22:// 中文

 

 范围:

● GBK/2: GB2312 汉字

B0 0 1 2 3 4 5 6 7 8 9 A B C D E F
A   啊 阿 埃 挨 哎 唉 哀 皑 癌 蔼 矮 艾 碍 爱 隘
B 鞍 氨 安 俺 按 暗 岸 胺 案 肮 昂 盎 凹 敖 熬 翱
C 袄 傲 奥 懊 澳 芭 捌 扒 叭 吧 笆 八 疤 巴 拔 跋
D 靶 把 耙 坝 霸 罢 爸 白 柏 百 摆 佰 败 拜 稗 斑
E 班 搬 扳 般 颁 板 版 扮 拌 伴 瓣 半 办 绊 邦 帮
F 梆 榜 膀 绑 棒 磅 蚌 镑 傍 谤 苞 胞 包 褒 剥

。。。
F7 0 1 2 3 4 5 6 7 8 9 A B C D E F
A   鳌 鳍 鳎 鳏 鳐 鳓 鳔 鳕 鳗 鳘 鳙 鳜 鳝 鳟 鳢
B 靼 鞅 鞑 鞒 鞔 鞯 鞫 鞣 鞲 鞴 骱 骰 骷 鹘 骶 骺
C 骼 髁 髀 髅 髂 髋 髌 髑 魅 魃 魇 魉 魈 魍 魑 飨
D 餍 餮 饕 饔 髟 髡 髦 髯 髫 髻 髭 髹 鬈 鬏 鬓 鬟
E 鬣 麽 麾 縻 麂 麇 麈 麋 麒 鏖 麝 麟 黛 黜 黝 黠
F 黟 黢 黩 黧 黥 黪 黯 鼢 鼬 鼯 鼹 鼷 鼽 鼾 齄

2

正则表达式

正则表达式30分钟入门教程

     转载来源 版本:v2.33 (2013-1-10) 作者:deerchao 

Get XRegExp 2.0: minified (3.5 KB gzipped), or with comments. Get the full package or the latest development build at GitHub.

Java中正则,中链接 Regular Expressions of Java Tutorial

Java正则表达式教程

<!--<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<!-- saved from url=(0029)http://tool.chinaz.com/ -->
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>正则表达式在线测试 - 站长工具</title>
    <meta name="keywords" content="正则表达式在线测试,正则表达式测试工具">
    <meta name="description" content="该工具主要针对程序开发人员,通过该工具可以快速准备的判断所写的正则是否能正确匹配相应的字符">
    <link rel="icon" href="http://tool.chinaz.com/Chinaz.ico" type="image/x-icon">
    <!--<link href="http://tool.chinaz.com/template/default/styles/toolsite.css?ver=2011_11" rel="stylesheet" type="text/css">-->
    <!--<script src="http://tool.chinaz.com/template/default/js/globals.js?ver=2011_10" type="text/javascript"></script>-->
    <!--<link rel="Stylesheet" type="text/css" href="http://tool.chinaz.com/template/default/styles/topbar.css">-->
    <script language="JavaScript">
        //if (self != top) { top.location = self.location; }
    </script>
    <style type="text/css">
        ul, ol, li, dl, dd, p, h1, h2, h3, h4, h5, h6, form, fieldset {
     
            margin: 0;
            padding: 0;
        }

        h1, h2, h3, h4, h5, h6 {
     
            font-size: 1em;
        }

        ul, ol {
     
            list-style: none;
        }

        img {
     
            vertical-align: middle;
            border: 0;
        }

        a {
     
            color: #0d8c21;
            text-decoration: none;
        }

            a:hover {
     
                color: red;
                text-decoration: underline;
            }

        select {
     
            font-size: 15px;
        }

        .blue {
     
            color: #3333ff;
        }

        .fl {
     
            float: left;
        }

        .fr {
     
            float: right;
        }

        .box h1 {
     
            line-height: 37px;
            height: 37px;
            padding-left: 20px;
            background: url("http://tool.chinaz.com/template/default/images/h1-bg.gif") repeat-x;
            color: #0066CC;
            border: 1px solid #c5e2f2;
            border-bottom: 0;
            font-size: 14px;
            font-weight: normal;
        }

            .box h1 span {
     
                float: right;
                background: url("http://tool.chinaz.com/template/default/images/h1.gif") no-repeat right;
                padding: 10px 12px 0 0;
                font-weight: normal;
            }

            .box h1 a {
     
                color: #3333ff;
            }

        .box .titright {
     
            float: right;
            padding-right: 10px;
        }

        .box .titleft {
     
            float: left;
        }

        .box .notice {
     
            color: red;
            margin-bottom: 5px;
            background: none repeat scroll 0% 0% transparent;
            border: 1px solid rgb(197, 226, 242);
        }

        .clear {
     
            clear: both;
            font-size: 0;
            line-height: 0;
            height: 10px;
        }

        .input {
     
            border: 1px solid #94c6e1;
            background: #fff;
            color: #22ac38;
            font-weight: bold;
            padding: 5px;
            margin-bottom: 5px;
        }

        .input {
     
            font-size: 13px;
        }

        .but {
     
            width: 90px;
            border: 1px solid #c5e2f2;
            background: #cde4f2 url('http://tool.chinaz.com/template/default/images/but.gif') repeat-x 50% top;
            height: 30px;
            margin-left: 5px;
            cursor: pointer;
            margin-bottom: 5px;
        }

        .but2 {
     
            border: 1px solid #c5e2f2;
            background: #cde4f2 url('http://tool.chinaz.com/template/default/images/but.gif') repeat-x 50% top;
            height: 30px;
            margin-left: 5px;
            cursor: pointer;
            margin-bottom: 5px;
            width: 90px;
        }

        .but3 {
     
            border: 1px solid #c5e2f2;
            background: #cde4f2 url('http://tool.chinaz.com/template/default/images/but.gif') repeat-x 50% top;
            height: 30px;
            margin-left: 5px;
            cursor: pointer;
            margin-bottom: 5px;
            width: 50px;
        }

        .but4 {
     
            border: 1px solid #c5e2f2;
            background: #cde4f2 url('http://tool.chinaz.com/template/default/images/but.gif') repeat-x 50% top;
            height: 30px;
            margin-left: 5px;
            cursor: pointer;
            margin-bottom: 5px;
            width: 120px;
        }

        .input1 {
     
            border: 1px solid #7f9db9;
            background: #fff;
            color: #333;
            font-weight: bold;
            padding: 3px 5px;
            margin-bottom: 5px;
        }

        .but1 {
     
            border: 1px solid #7f9db9;
            background: #f0f7fd;
            height: 23px;
            margin-left: 5px;
            cursor: pointer;
            overflow: visible;
            padding: 0 15px;
            margin-bottom: 5px;
        }
        /*w4648*/
        . {
     
            margin: auto;
            width: 900px;
            clear: both;
        }

        td, th {
     
            border: 1px solid #C0C0C0;
            border-collapse: collapse;
            padding: 5px;
        }

        table {
     
            border-collapse: collapse;
            border: 1px solid #C0C0C0;
            margin: 0 auto;
        }

        .menu-list {
     
            z-index: 5;
        }

        #mainbody {
     
            padding-top: 10px;
            padding-bottom: 10px;
        }

        #condition ul li {
     
            float: left;
        }

        #search {
     
            height: 180px;
            width: 99.75%;
        }

        #input {
     
            height: 375px;
            margin-top: 10px;
            width: 99.75%;
        }

        .smartField {
     
            border: 1px solid #CCCCCC;
            overflow: auto;
            position: relative;
        }

            .smartField pre, .smartField textarea {
     
                width: 100%;
                padding: 0;
                margin: 0;
                font: 100% "courier new",monospace;
            }

            .smartField pre {
     
                text-align: left;
                color: #F9F9F9;
                z-index: 1;
            }

            .smartField textarea {
     
                background: none repeat scroll 0 0 transparent;
                border: 0 none;
                height: 100%;
                overflow: hidden;
                position: absolute;
                left: 0px;
                top: 0px;
                z-index: 2;
            }

        b, i, u {
     
            font-style: normal;
            font-weight: normal;
            text-decoration: none;
        }

        #input b {
     
            background: none repeat scroll 0 0 #FFF000;
            color: #FFF000;
        }

        #input i {
     
            background: none repeat scroll 0 0 #80C0FF;
            color: #80C0FF;
        }

        #search b {
     
            background: none repeat scroll 0 0 #AAD1F7;
            color: #AAD1F7;
        }

        #search i {
     
            background: none repeat scroll 0 0 #F9CA69;
            color: #F9CA69;
        }

            #search i b {
     
                background: none repeat scroll 0 0 #F7A700;
                color: #F7A700;
            }

            #search i u {
     
                background: none repeat scroll 0 0 #EFBA4A;
                color: #EFBA4A;
            }

        #search b.g1 {
     
            background: none repeat scroll 0 0 #D2F854;
            color: #D2F854;
        }

        #search b.g2 {
     
            background: none repeat scroll 0 0 #9EC70C;
            color: #9EC70C;
        }

        #search b.g3 {
     
            background: none repeat scroll 0 0 #ECC9F7;
            color: #ECC9F7;
        }

        #search b.g4 {
     
            background: none repeat scroll 0 0 #54B70B;
            color: #54B70B;
        }

        #search b.g5 {
     
            background: none repeat scroll 0 0 #B688CF;
            color: #B688CF;
        }

        #search b.err {
     
            background: none repeat scroll 0 0 #FF4300 !important;
            color: #FF4300 !important;
        }
    </style>
</head>
<body>
    <div class="w4648">
        <!--main-->
        <div class="main">
            <div class="box">
                <div id="b_1">
                    <h1><a style="color: #3333ff;" href="http://tool.chinaz.com/regex/">正则表达式在线测试</a></h1>
                    <div class="box1" style="text-align:center;">
                        <div id="condition">
                            <ul>
                                <li style=" display:none;"><input type="checkbox" checked="checked" id="toolG"><label for="toolG">全局</label></li>
                                <li><input type="checkbox" id="toolI"><label for="toolI">不区分大小写</label></li>
                                <li><input type="checkbox" id="toolM"><label for="toolM">对^$前后换行也支持</label></li>
                                <li><input type="checkbox" id="toolS"><label for="toolS">符号.匹配所有</label></li>
                            </ul>
                            <span><input type="checkbox" checked="checked" id="highSyntax"><label for="highSyntax">对正则着色</label></span>
                            <span><input type="checkbox" checked="checked" id="highMatch"><label for="highMatch">对匹配结果着色</label></span>
                            <span><input type="checkbox" id="invertMatch"><label for="invertMatch">对无匹配结果着色</label></span>
                        </div>
                        <div id="mainbody">
                            <div class="smartField" id="search">
                                <textarea spellcheck="false" tabindex="1" rows="3" cols="100" id="searchText" style="height: 180px; margin-left: 0px; width: 856px;">(\w+\.){2}\w+</textarea>
                            </div>
                            <div class="smartField" id="input" style="height: 180px;">
                                <textarea spellcheck="false" tabindex="2"
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值