美文网首页地震数据专家[DataEQ]
DatistEQ之抽取文本内容

DatistEQ之抽取文本内容

作者: 了无_数据科学 | 来源:发表于2022-04-09 12:57 被阅读0次

    V 2022 Q2版

    数据处理过程中,时常会遇到从字符串中提取特定内容的场景,如从FTP的连接串中,抽取用户名。

    ftp连接串:ftp://anonymous:guest@ftp.ncbi.nlm.nih.gov:125/gdgdsg/wrwq
    

    在C语言中可以用scanf函数,在DatistEQ如何实现呢?

    DatistEQ中,提供MatchGroup和MatchGroups两个函数,使用正则表达式,抽取一个或一组数据。

    MatchGroup函数格式:

    string MatchGroup(string,regexString,[Optional] groupNames,[Optional] regexOptions)
    说明:
    分组正则表达式匹配,返回第一个匹配结果。
    string,源文本字符串;
    regexString,正则表达式;
    groupNames,可选项,默认为空,多个组名以|分隔;
    RegexOptions,可选项,默认为Compiled|IgnoreCase,用于设置正则表达式选项的枚举值。支持:None,Compiled,CultureInvariant,ECMAScript,ExplicitCapture,
    IgnoreCase,IgnorePatternWhitespace,Multiline,RightToLeft,Singleline。
    

    MatchGroups函数格式与MatchGroup相似:

    list MatchGroups(string,regexString,[Optional] groupNames,[Optional] regexOptions)
    说明:
    分组正则表达式匹配,返回所有匹配结果。 
    
    对比案例1:未分组正则表达式
    MatchGroup( "abc123def12344" , "[0-9]+")
    输出:123
    
    MatchGroups( "abc123def12344" , "[0-9]+")
    输出:
    [
        "123", //第一组
        "12344" //第二组
    ]
    
    对比案例2:分组正则表达式,未指定提取分组名称
    MatchGroup( "abc123def12344" , "(?<a1>[0-9]+)")
    输出:
    {
        "0": "123",
        "a1": "123"
    }
    
    MatchGroups( "abc123def12344" , "(?<a1>[0-9]+)")
    输出:
    [
        { //第一组
            "0": "123", //系统默认,未指定组名
            "a1": "123"
        },
        {//第二组
            "0": "12344", //系统默认,未指定组名
            "a1": "12344"
        }
    ]
    
    对比案例3:分组正则表达式,指定一个提取分组名称
    MatchGroup( "abc123:45def12344:78" , "(?<a1>[0-9]+):(?<b1>[0-9]+)","a1" )
    输出:123
    
    MatchGroups( "abc123:45def12344:78" , "(?<a1>[0-9]+):(?<b1>[0-9]+)","a1" )
    输出:
    [
        "123", //第一组
        "12344" //第二组
    ]
    
    对比案例4:分组正则表达式,指定多个提取分组名称
    MatchGroup( "abc123:45def12344:78" , "(?<a1>[0-9]+):(?<b1>[0-9]+)","a1|b1" )
    输出:
    {
        "a1": "123",
        "b1": "45"
    }
    
    MatchGroups( "abc123:45def12344:78" , "(?<a1>[0-9]+):(?<b1>[0-9]+)","a1|b1" )
    输出:
    [
        {//第一组
            "a1": "123",
            "b1": "45"
        },
        { //第二组
            "a1": "12344",
            "b1": "78"
        }
    ]
    
    
    
    例1:
    MatchGroup("关井油压5.7MPa,套压8.2MPa。", "油压(?<P1>[0-9]+(\.[0-9]+){0,1})" ,"P1")
    输出:5.7
    
    例2:
    MatchGroup("关井油压5.7MPa,套压8.2MPa。", "油压(?<P1>[1-9]\d*.\d*|0.\d*[1-9]\d*)[\s\S]*套压(?<P2>[1-9]\d*.\d*|0.\d*[1-9]\d*)" ,"P1|P2")
    输出:
    {
        "P1": "5.7",
        "P2": "8.2"
    }
    
    例3:
    MatchGroup( "ftp://anonymous:guest@ftp.ncbi.nlm.nih.gov:21/gdgdsg/wrwq" , "(?<ftp>sftp|ftp)?://((?<username>\w+):(?<password>\w+)@)*(?<url>[.A-Za-z0-9]*)(:(?<port>[0-9]*))*(?<remotepath>/[\s\S]*)*","ftp|username|password|url|port|remotepath" )
    输出:
    {
        "ftp": "ftp",
        "username": "anonymous",
        "password": "guest",
        "url": "ftp.ncbi.nlm.nih.gov",
        "port": "21",
        "remotepath": "/gdgdsg/wrwq"
    }
    

    后记:
    MatchGroup函数配合“解析JSON”节点,分隔字段内容。

    对象 对象数组 数值数组

    相关文章

      网友评论

        本文标题:DatistEQ之抽取文本内容

        本文链接:https://www.haomeiwen.com/subject/mehpsrtx.html