Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.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/8/grails/5.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_Grails_Groovy - Fatal编程技术网

Regex 正则表达式匹配<;标题></标题>;包括新线在任何地方

Regex 正则表达式匹配<;标题></标题>;包括新线在任何地方,regex,grails,groovy,Regex,Grails,Groovy,我正试图编写一个正则表达式来从URL中提取,但问题是“.”与我们已经知道的换行符不匹配。如何编写正则表达式来匹配和提取pageTitle(.*),但换行符可以位于这两者之间的任意位置 我正在使用grails。假设它是用于PHP的: preg_match( "#<title>(.*?)</title>#s", $source, $match ); $title = $match[1]; preg#u match(#(.*)#s“,$source,$match); $tit

我正试图编写一个正则表达式来从URL中提取,但问题是“.”与我们已经知道的换行符不匹配。如何编写正则表达式来匹配和提取pageTitle(.*),但换行符可以位于这两者之间的任意位置

我正在使用grails。

假设它是用于PHP的:

preg_match( "#<title>(.*?)</title>#s", $source, $match );
$title = $match[1];
preg#u match(#(.*)#s“,$source,$match);
$title=$match[1];
无论您使用的是什么软件,添加
s
扩展将修改
(任何字符),使其包含换行符。

假设它是用于PHP的:

preg_match( "#<title>(.*?)</title>#s", $source, $match );
$title = $match[1];
preg#u match(#(.*)#s“,$source,$match);
$title=$match[1];

无论您使用的是什么软件,添加
s
扩展都会修改
(任何字符),使其包含换行符。

虽然您不能使用正则表达式解析通用HTML,但在这种情况下,您可能可以不使用它。在Groovy中,可以使用
(?s)
操作符使点匹配换行符。您可能还应该使用
(?i)
操作符使正则表达式不区分大小写。您可以将它们组合为
(?is)

比如说

def titleTagWithNoLineBreaks = "<title>This is a title</title>"
def titleTagWithLineBreaks = """<title>This is
a title</title>"""

// Note the (?is) at the beginning of the regex
// The 'i' makes the regex case-insensitive
// The 's' make the dot match newline characters
def pattern = ~/(?is)<title>(.*?)<\/title>/

def matcherWithNoLineBreaks = titleTagWithNoLineBreaks =~ pattern
def matcherWithLineBreaks = titleTagWithLineBreaks =~ pattern

assert matcherWithNoLineBreaks.size() == 1
assert matcherWithLineBreaks.size() == 1

assert matcherWithLineBreaks[0][1].replaceAll(/\n/,' ') == "This is a title"
def titleTagWithNoLineBreaks=“这是一个标题”
def titleTagWithLineBreaks=“”这是
头衔
//注意(?is)在正则表达式的开头
//“i”使正则表达式不区分大小写
//“s”使点与换行符匹配
def模式=~/(?is)(*)/
def matcherWithNoLineBreaks=标题标签WithNolineBreaks=~pattern
def matcherWithLineBreaks=标题标签WithLineBreaks=~pattern
断言matcherWithNoLineBreaks.size()==1
断言matcherWithLineBreaks.size()==1
断言matcherWithLineBreaks[0][1]。replaceAll(/\n/,“”)==“这是一个标题”

希望这能有所帮助。

虽然您不能使用正则表达式来解析一般的HTML,但在这种情况下,您可能不需要使用正则表达式。在Groovy中,可以使用
(?s)
操作符使点匹配换行符。您可能还应该使用
(?i)
操作符使正则表达式不区分大小写。您可以将它们组合为
(?is)

比如说

def titleTagWithNoLineBreaks = "<title>This is a title</title>"
def titleTagWithLineBreaks = """<title>This is
a title</title>"""

// Note the (?is) at the beginning of the regex
// The 'i' makes the regex case-insensitive
// The 's' make the dot match newline characters
def pattern = ~/(?is)<title>(.*?)<\/title>/

def matcherWithNoLineBreaks = titleTagWithNoLineBreaks =~ pattern
def matcherWithLineBreaks = titleTagWithLineBreaks =~ pattern

assert matcherWithNoLineBreaks.size() == 1
assert matcherWithLineBreaks.size() == 1

assert matcherWithLineBreaks[0][1].replaceAll(/\n/,' ') == "This is a title"
def titleTagWithNoLineBreaks=“这是一个标题”
def titleTagWithLineBreaks=“”这是
头衔
//注意(?is)在正则表达式的开头
//“i”使正则表达式不区分大小写
//“s”使点与换行符匹配
def模式=~/(?is)(*)/
def matcherWithNoLineBreaks=标题标签WithNolineBreaks=~pattern
def matcherWithLineBreaks=标题标签WithLineBreaks=~pattern
断言matcherWithNoLineBreaks.size()==1
断言matcherWithLineBreaks.size()==1
断言matcherWithLineBreaks[0][1]。replaceAll(/\n/,“”)==“这是一个标题”

希望能有所帮助。

如果您只需要解析可能格式错误的HTML文档,可以尝试使用解析器。然后,您可以只使用GPath表达式,而不必担心标题中的注释中出现诸如“”之类的奇怪情况

import org.ccil.cowan.tagsoup.Parser

final parser  = new Parser()
final slurper = new XmlSlurper(parser)
final html    = slurper.parse('http://www.example.com/')

println html.depthFirst().find { it.name() == 'title' }

如果您只需要解析可能格式错误的HTML文档,那么可以尝试使用解析器。然后,您可以只使用GPath表达式,而不必担心标题中的注释中出现诸如“”之类的奇怪情况

import org.ccil.cowan.tagsoup.Parser

final parser  = new Parser()
final slurper = new XmlSlurper(parser)
final html    = slurper.parse('http://www.example.com/')

println html.depthFirst().find { it.name() == 'title' }

嗯,你有没有可能尝试用正则表达式解析HTML?无论是希望还是失败,都为时已晚。圣杯与此有什么关系?也许你是指Groovy?嗯,你有没有可能尝试用正则表达式解析HTML?无论是希望还是失败,都为时已晚。圣杯与此有什么关系?也许你是说Groovy?我应该在我的模式末尾的哪里添加“s”?它在groovy中不起作用。使用分隔符将正则表达式放入,并将
s
放在正则表达式的末尾。@Dark Slipstream,你为什么要鼓励可怜的灵魂使用正则表达式解析HTML来将其遗忘?我已经尝试过使用XMLParser,但有些网站的格式不好。@toy,你为什么要使用一个格式不好的网站?找一个替代方案。就我个人而言,如果一个网站不遵守web标准,我就不会在它身上下赌注,更不用说基于它的任何开发了。我应该在我的模式末尾添加“s”吗?它在groovy中不起作用。使用分隔符将正则表达式放入,并将
s
放在正则表达式的末尾。@Dark Slipstream,你为什么要鼓励可怜的灵魂使用正则表达式解析HTML来将其遗忘?我已经尝试过使用XMLParser,但有些网站的格式不好。@toy,你为什么要使用一个格式不好的网站?找一个替代方案。就我个人而言,如果一个网站不遵守网络标准,我就不会把我的钱押在它身上,更不用说基于它的任何开发了。