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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/25.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
Regex 正则表达式-匹配所有可能的字符和空格_Regex - Fatal编程技术网

Regex 正则表达式-匹配所有可能的字符和空格

Regex 正则表达式-匹配所有可能的字符和空格,regex,Regex,我想从html中提取数据。问题是,我无法提取模式顶部和底部的两个字符串 我想提取23423423和1234523453245,但仅当以下两者之间存在字符串Allan时: <h4><a href="/Profile/23423423423.html">@@@@@@</a> </h4> said12:49:32 </div>

我想从html中提取数据。问题是,我无法提取模式顶部和底部的两个字符串

我想提取
23423423
1234523453245
,但仅当以下两者之间存在字符串
Allan
时:

                                        <h4><a href="/Profile/23423423423.html">@@@@@@</a>  </h4> said12:49:32
            </div>

                                <a href="javascript:void(0)" onclick="replyAnswer(@@@@@@@@@@,'GET','');" class="reportLink">
                    report                    </a>
                        </div>

        <div class="details">
                            <p class="content">


                       Hi there, Allan.



                                </p>

            <div id="AddAnswer1234523453245"></div>
said12:49:32

你好,艾伦。

当然,我可以这样做:
Profile\/(\d+).*\s*\s*\s*\s*\s*\s*\s**Allan.*\s*\s*\s**添加答案(\d+)
)。但是代码很可怕。有没有办法把它缩短

我在想:

Profile\/(\d+)(\sAllan)*添加答案(\d+)

Profile\/(\d+)(*Allan\s*)*添加答案(\d+)


但是没有一个能正常工作。你有什么想法吗?

你可以使用
m
指定
来匹配换行符


/Profile\/(\d+)+AddAnswer(\d+)/m

您可以通过使用
[\S\S]
构造一个字符组来匹配任何字符,包括换行符。所有空格和非空格字符均为所有字符

那么,你的尝试就相当接近了

/Profile\/(\d+)[\S\s]*Allan[\S\s]*AddAnswer(\d+)/
这将查找配置文件、它后面的数字、Allan前面的任何字符、AddAnswer之前的任何字符以及它后面的数字。如果有单线模式可用(
/s
),则可以使用点

/Profile\/(\d+).*Allan.*AddAnswer(\d+)/s

。如果出于任何原因必须使用正则表达式,则可以使用以下方法:


请参见a

您使用哪种编程语言?我使用的是iMacros。但是它与Regex101.com一样(我认为),这是完全错误的-
s
用于单行模式,
m
用于多行匹配
^
$
。这不适用于多个实例(它只捕获最后一个),请参阅我的答案以获得更好的解决方案。我相信使用非贪婪匹配的正则表达式可能会执行得更好:
/Profile\/(\d+)[\s\s]*?Allan[\s\s]*?(\d+)/g
正则表达式101显示匹配模式需要9110个步骤,而使用此非贪婪匹配模式只需要2740个步骤。
Profile/(\d+)            # Profile followed by digits
(?:(?!Allan)[\S\s])+     # any character except when there's Allan ahead
Allan                    # Allan literally
(?:(?!AddAnswer)[\S\s])+ # same construct as above
AddAnswer(\d+)           # AddAnswer, followed by digits