Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
sql:用于特定模式分离的正则表达式_Sql_Regex_Oracle - Fatal编程技术网

sql:用于特定模式分离的正则表达式

sql:用于特定模式分离的正则表达式,sql,regex,oracle,Sql,Regex,Oracle,在表“example”中,我有一列“col1”,其中包含以下字符串 some example text here x2.0.3-a abc some other example text 1.5 abc another example text 0.1.4 mnp some other example text abc another example text mnp 现在我需要以下东西 将之前的零件添加到另一列“col1” 将零件添加到另一列“col

在表“example”中,我有一列“col1”,其中包含以下字符串

    some example text here x2.0.3-a abc
    some other example text 1.5 abc
    another example text 0.1.4 mnp
    some other example text  abc
    another example text mnp
现在我需要以下东西

  • 之前的零件添加到另一列“col1”
  • 将零件添加到另一列“col2”
  • 所以输出应该是这样的

           col1                  col2
    some example text here      x2.0.3-a
    some other example text     1.5 
    another example text        0.1.4 
    some other example text 
    another example text 
    
    col1中字符串的某些属性是

  • col1中的字符串始终以
    abc
    mnp
  • 像这样的数字
    x2.0.3-a
    0.1.4
    是属性。这些属性可能并不总是存在于col1字符串中。但是如果它退出,那么它总是在结束字符串
    abc
    mnp
    之前存在
  • 属性前和属性后始终有一个空格,即结束字符串abc/mnp和属性之间的另一个空格
  • 所以我的问题是如何分离属性并将它们添加到col2中?
    我脑海中浮现的一个想法是,试着用
    *.*abc/mnp
    *.*abc/mnp
    找到一些东西,那就是任何
    任何
    空间
    abc/mnp
    或任何
    任何
    任何空间
    abc/mnp
    。我不确定我解释得是否正确

    据我所知,您希望将您的列拆分为3列。您应该更好地解释第二列的范围和语义,以便确保正则表达式定期与之匹配

    我构建了一个与您提供的数据并行的正则表达式,因此它可能与将来的传入线路不匹配。正则表达式就在这里:它所做的是捕获三个主要组:

    (.+?)\s?([a-z]?\d(?:\.\d){1,2}(?:-[a-z])?)?\s(abc|mnp)
    
    让我们将正则表达式分成几个部分:

  • (.+?)
  • \s
  • ([a-z]?\d(?:.\d){1,2}(?:-[a-z])?)
  • \
  • (abc | mnp)
  • 从相反的顺序开始,第五部分简单地匹配abc或mnp。第四部分期待一个空间。第三部分与第二列匹配(如果存在),请注意,此部分用于您提供的内容,以便您可以修改此部分以更好地适应您的数据。第二部分需要一个空格(如果存在),这是用于包含空第二列的行。第一部分是为其余部分


    据我所知,在Oracle中,我们使用正则表达式具有搜索和子字符串函数。因此,您需要一种编程语言来捕获这些组

    为此,我编写了一个Java方法:

    static List<String> getGroups(String content, String regex){
    
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(content);
    
        List<String> groupsMatched = new ArrayList<String>();
    
        if(matcher.find()){
            for(int i=0; i<matcher.groupCount(); i++)               
                groupsMatched.add(matcher.group(i));
    
            return groupsMatched; 
        }else
            return null;
    }
    
    希望这有帮助


    干杯,

    您正在使用什么RDBMS?数据库对正则表达式的处理方式各不相同。我使用的是Oracle2。你能详细介绍一下关于col2数据的信息吗?除了您提供的x3.1-a或version4.5之外,还会有其他模式吗?“在Oracle中,据我所知,我们有带正则表达式的搜索和子字符串函数”--您的意思是说“我们没有”?Oracle在我说“据我所知,我们有带正则表达式的搜索和子字符串函数”,我的意思是我们有REGEXP_like(),REGEXP_INSTR(),REGEXP_REPLACE()和REGEXP_SUBSTR()函数。我没有使用任何否定。然而,如果想要得到匹配模式的组,我不知道Oracle中是否有方法。
    for(String content : listOfContent){            
    
            List<String> groupsMatched = getGroups(content, regex);
    
            if(groupsMatched != null)           
                System.out.println(groupsMatched.get(1) + "\t" + groupsMatched.get(2) + "\t" + groupsMatched.get(3) );
    
        }
    
    some example text here   x2.0.3-a   abc
    some other example text  1.5        abc
    another example text     0.1.4      mnp
    some other example text  null       abc
    another example text     null       mnp