标签: regex-group

php捕获组

我有点困难preg_match()在php中捕获一个组.

这是我的模式:

<ns2:uniqueIds>(.*)<\/ns2:uniqueIds>
Run Code Online (Sandbox Code Playgroud)

这是来源:

<env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope"><env:Header/><env:Body><ns2:ListResponse xmlns:ns2="http://censored"><ns2:uniqueIds>censored</ns2:uniqueIds><ns2:uniqueIds>censored</ns2:uniqueIds></ns2:ListResponse></env:Body></env:Envelope>
Run Code Online (Sandbox Code Playgroud)

我错过了什么?

php preg-match regex-group

1
推荐指数
1
解决办法
4865
查看次数

简单的python正则表达式组无法解析日期

我正在尝试使用正则表达式解析日期,使用组,但python返回空列表.我没有做任何花哨的事情,只是12/25/10的东西.我希望它拒绝12/25-10.

date = re.compile("\d{1,2}([/.-])\d{1,2}\1\d{2}")
Run Code Online (Sandbox Code Playgroud)

我已经尝试过在线正则表达式库,但他们的解决方案似乎也没有运行.有任何想法吗?

样本输入:"你好今天是10/18/10,时间是10:50 am"希望输出:"10/18/10"

我正在运行Python 2.5.

python regex python-2.x regex-group

1
推荐指数
2
解决办法
846
查看次数

java.util.regex.Matcher困惑组

我无法获得正确的正则表达式匹配组.我的代码归结为以下内容:

Pattern fileNamePattern = Pattern.compile("\\w+_\\w+_\\w+_(\\w+)_(\\d*_\\d*)\\.xml");
Matcher fileNameMatcher = fileNamePattern.matcher("test_test_test_test_20110101_0000.xml");

System.out.println(fileNameMatcher.groupCount());

if (fileNameMatcher.matches()) {
    for (int i = 0; i < fileNameMatcher.groupCount(); ++i) {
        System.out.println(fileNameMatcher.group(i));
    }
}
Run Code Online (Sandbox Code Playgroud)

我希望输出为:

2
test
20110101_0000
Run Code Online (Sandbox Code Playgroud)

它然而:

2
test_test_test_test_20110101_0000.xml
test
Run Code Online (Sandbox Code Playgroud)

有人有解释吗?

java regex matcher regex-group

1
推荐指数
1
解决办法
2876
查看次数

Sed正则表达式不匹配'或'或'内部组

我想匹配使用sed和regex通过管道传递的多个文件扩展名.

以下作品:

sed '/.\(rb\)\$/!d'
Run Code Online (Sandbox Code Playgroud)

但是,如果我想允许多个文件扩展名,下面也不能正常工作.

sed '/.\(rb\|js\)\$/!d'
sed '/.\(rb|js\)\$/!d'
sed '/.(rb|js)\$/!d'
Run Code Online (Sandbox Code Playgroud)

关于如何做/或内组的任何想法?

这是整个代码块:

#!/bin/sh
files=`git diff-index --check --cached $against | # Find all changed files
       sed '/.\(rb\|js\)\$/!d'                  | # Only process .rb and .js files
       uniq`                                      # Remove duplicate files
Run Code Online (Sandbox Code Playgroud)

regex sed regex-group

1
推荐指数
1
解决办法
794
查看次数

PHP preg_match_all命名组问题

我正在尝试从以下URI获取分组匹配:

route: "/user/{user}/{action}"
input: "/user/someone/news"
Run Code Online (Sandbox Code Playgroud)

什么是适当的正则表达式?在过去的几个小时里,我一直在寻找自己的酸...

我尝试过类似的东西,但没有结果:(

~\/app\/user\/(?P<user>[.*]+)\/(?P<action>[.*]+)~
Run Code Online (Sandbox Code Playgroud)

我在匹配数组中返回组,但没有基于组内输入的结果.

期望的输出:

Array
(
    [0] => Array
        (
            [0] => "someone"
        )

    [user] => Array
        (
            [0] => "someone"
        )

    [1] => Array
        (
            [0] => "news"
        )

    [action] => Array
        (
            [0] => "news"
        )
)
Run Code Online (Sandbox Code Playgroud)

通过一个例子澄清:

我的控制器有以下路由:/app/user/{username}/{action} 来自浏览器的请求URI是:/app/user/john/news

在捕获括号之间的变量时,如何使用正则表达式模式将该请求URI与该路由匹配?

php regex preg-match-all regex-group

1
推荐指数
1
解决办法
827
查看次数

为什么会出现java.lang.IllegalArgumentException?

java.lang.IllegalArgumentException每当我运行它时,我的程序就会打印出来.它用不同的表达式替换某些模式,包括匹配的模式中的组.它取代了部分模式,然后出现此错误:

Exception in thread "main" java.lang.IllegalArgumentException: Illegal group reference
    at java.util.regex.Matcher.appendReplacement(Matcher.java:713)
    at RealReadFile.main(RealReadFile.java:93)
Run Code Online (Sandbox Code Playgroud)

这是我的代码:

import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.io.FileNotFoundException;
import java.io.File;

public class RealReadFile {
    private static final String fileName = "KLSadd.tex";
    private Scanner myFile = null;

    public RealReadFile() throws FileNotFoundException {
        if (myFile == null)
            myFile = new Scanner(new File(fileName));
    }

    public RealReadFile(String name) throws FileNotFoundException {
        if (myFile != null)
            myFile.close();
        myFile = new Scanner(new File(name));
    }

    public boolean endOfFile() { 
        return !myFile.hasNext(); 
    } …
Run Code Online (Sandbox Code Playgroud)

java regex exception illegalargumentexception regex-group

1
推荐指数
1
解决办法
2158
查看次数

Sed捕获组无法正常工作

这似乎是sed捕获组中最简单的可能示例之一,但它不起作用.我已经在在线正则表达式测试中测试了正则表达式,并且做了我想要的.在linux命令行,它没有.

$ echo "a 10 b 12" | sed -E -n 's/a ([0-9]+)/\1/p'
$
Run Code Online (Sandbox Code Playgroud)

$ echo "a 10 b 12" | sed -E -n 's/a ([0-9]+)/\1/p'
10 b 12
Run Code Online (Sandbox Code Playgroud)

https://regex101.com/r/WS3lG9/1

我希望能抓住"10".

regex bash sed regex-group

1
推荐指数
1
解决办法
3070
查看次数

剥去长度小于或大于6的所有数字

大家好,祝你有个美好的一天.我想问一些愚蠢的问题,因为我正在努力寻找正确的模式.我想找到小于或大于长度6的所有数字字符.

我们来看看下面.例如,我有这个序列

12134 4aRt32212121a 11111111111 222222 asda383652re5
Run Code Online (Sandbox Code Playgroud)

我的解决方案是这个

\b[0-9]{1,5}\b|\b[0-9]{7,20}\b
Run Code Online (Sandbox Code Playgroud)

我得到的匹配就是这个

12134 11111111111
Run Code Online (Sandbox Code Playgroud)

我的问题是我的正则表达式与这里的任何数字都不匹配4aRt32212121a.我想要的比赛就是这个

 12134 4 32212121 11111111111 5
Run Code Online (Sandbox Code Playgroud)

由于长度数等于6,因此将从匹配中排除的数字为222222,383652

我使用这个Regex在线工具进行了测试.如果可能,你可以在这里做例子.我会非常感激任何帮助,并再次原谅我的愚蠢

java regex regex-group

1
推荐指数
1
解决办法
52
查看次数

正则表达式用于匹配Yaml文件

我有包含yaml文件的下一个目录路径:

test/1.yaml
test/dev.yaml
test/dev0_r.yaml 
Run Code Online (Sandbox Code Playgroud)

等等

如何匹配完全在test /目录中但不在子目录(如test / test1 / dev.yaml)中的所有Yaml文件

我正在尝试使用globing:

test/*.yaml 
Run Code Online (Sandbox Code Playgroud)

但在https://regex101.com/上不起作用

我该如何实现?

regex string regex-negation regex-group regex-greedy

1
推荐指数
1
解决办法
75
查看次数

正则表达式捕获未返回预期的数组

我正在尝试使用正则表达式生成一组捕获组,但是捕获组没有捕获我期望的字符串。

我的输入类似于以下常规格式:

S2244060800027008209302B000A667A6201A6828E1976007A000A52820009A3420009B0FE1F
Run Code Online (Sandbox Code Playgroud)

该字符串具有以下组成部分:4个字符的前缀,3个字节的地址,一系列2字节的数据块,1个字节的行终止符。

我只关心地址和数据块。

我尝试使用的正则表达式是: /\w{4}(\w{6})(\w{4})+/;

(在执行正则表达式之前,我有一个使用子字符串解析输入的精巧解决方案,但我讨厌它……)

原始的非功能代码如下:

open IN, "<$ARGV[0]" or die "Could not open '$ARGV[0]': $!\n";
open OUT, ">$ARGV[0].txt" or die "Could not open '$ARGV[0].txt': $!\n";

while (<IN>)
{
    print OUT join(" ",/^\w{4}(\w{6})(\w{4})+/)."\n"; 
}
close IN;
close OUT;
Run Code Online (Sandbox Code Playgroud)

我想要的输出是一个包含以下元素的数组:

@array = [406080, 0027, 0082, 0930, 2B00, 0A66, 7A62, 01A6, 828E, 1976, 007A, 000A, 5282, 0009, A342, 0009, B0FE]
Run Code Online (Sandbox Code Playgroud)

但是前面的正则表达式会导致一个仅包含3个字节地址和最后一个数据块的两个元素数组:

@array = [406080, B0FE]
Run Code Online (Sandbox Code Playgroud)

对我而言,这意味着我对正则表达式捕获组的工作方式有一个基本的误解,我想澄清一下。

仅出于完整性考虑,这是我糟糕的解决方法:

    open IN, "<$ARGV[0]" or die "Could not open '$ARGV[0]': $!\n";    
    open OUT, …
Run Code Online (Sandbox Code Playgroud)

regex perl regex-group regex-greedy

1
推荐指数
1
解决办法
61
查看次数