Javascript 带有nodej和备选方案的正则表达式问题
我想用JavaScript识别带有正则表达式的足球比赛:Javascript 带有nodej和备选方案的正则表达式问题,javascript,regex,node.js,Javascript,Regex,Node.js,我想用JavaScript识别带有正则表达式的足球比赛: 1 15/06 16:00 Brasília Brasilien 3:0 (1:0) Japan 2 23/06 16:00 Recife Uruguay - Tahiti 该文本包含: 比赛日期和时间 比赛地点 两队 如果游戏已玩或未玩,则分数包含“-” 我已使用以下站点构建了一个正则表达式: 此正则表达式应捕获两个备选方案(有分数和无分数) 下面是整个测试内容的链接: 我的JavaScript代码与NodeJS: fu
1
15/06 16:00
Brasília
Brasilien
3:0 (1:0)
Japan
2
23/06 16:00
Recife
Uruguay
-
Tahiti
该文本包含:
- 比赛日期和时间
- 比赛地点
- 两队
- 如果游戏已玩或未玩,则分数包含“-”
function CreateMatchesFromString(data)
{
var re = /(\d\d\/\d\d)\s(\d\d:\d\d)\s(.+)\s\s\s(.+)\s(?:-|(\d):(\d)\s\(\d:\d\))\s(.+)/g;
var myArray;
while ((myArray = re.exec(data)) !== null)
{
console.log("date:"+ myArray[1]);
console.log("time:"+ myArray[2]);
console.log("place:"+ myArray[3]);
console.log("Home:"+ myArray[4]);
console.log("Away:"+ myArray[5]);
}
}
但是我没有得到客场球队,那就是第五组!
我的输出:
date:26/06
time:22:00
place:Curitiba
Home:Algerien
Away:undefined
只有当我没有用“|”替换表达式时,我才得到它:
或者当我使用“[”“]”而不是“(“and”)”对备选方案进行分组时
有什么问题?
这是不是因为忽略了最后一个捕获组而导致Nodejs正则表达式引擎中的错误!?或
正则表达式错了吗
致意
Michael您的问题可能只是捕获组的问题。
此正则表达式不会更改您的原始数据(我对您的数据了解不够)
只是改变了捕获组 编辑-这适用于您的测试数据。它是相同的正则表达式,但添加了一些空白部分
# /[^\S\r\n]*(?:\r?\n)(\d\d\/\d\d)[^\S\r\n]+(\d\d:\d\d)[^\S\r\n]*(?:\r?\n)(.+)(?:\r?\n)[^\S\r\n]*(?:\r?\n)(.+)(?:\r?\n)(?:-|(\d):(\d)[^\S\r\n]+\(\d:\d\))[^\S\r\n]*(?:\r?\n)(.+)/
[^\S\r\n]*
(?: \r? \n ) # linebreak
# ---------------
( \d\d / \d\d ) # (1), Date
[^\S\r\n]+
( \d\d : \d\d ) # (2), Time
[^\S\r\n]*
(?: \r? \n ) # linebreak
# ---------------
( .+ ) # (3), Place
(?: \r? \n ) # linebreak
# ---------------
[^\S\r\n]* # blank line
(?: \r? \n ) # linebreak
# ---------------
( .+ ) # (4), Home
(?: \r? \n ) # linebreak
# ---------------
(?:
- # No score
| # or,
( \d ) # (5), Score home
: # :
( \d ) # (6), Score away
[^\S\r\n]+
\( \d : \d \)
)
[^\S\r\n]*
(?: \r? \n ) # linebreak
# ---------------
( .+ ) # (7), Away
未测试的JS代码
var pattern = /[^\S\r\n]*(?:\r?\n)(\d\d\/\d\d)[^\S\r\n]+(\d\d:\d\d)[^\S\r\n]*(?:\r?\n)(.+)(?:\r?\n)[^\S\r\n]*(?:\r?\n)(.+)(?:\r?\n)(?:-|(\d):(\d)[^\S\r\n]+\(\d:\d\))[^\S\r\n]*(?:\r?\n)(.+)/g;
while ((match = pattern.exec( data )) != null)
{
console.log( "\n" );
console.log( "Date: " + match[1] + "\n";
console.log( "Time: " + match[2] + "\n";
console.log( "Place: " + match[3] + "\n";
console.log( "Home: " + match[4] + "\n";
console.log( "Away: " + match[7] + "\n";
console.log( "Score: ";
if (match[5] != null) {
console.log( match[5] + " to " + match[6] + "\n";
}
else {
console.log( "no info\n";
}
}
Perl测试用例
$/ = undef;
$str = <DATA>;
while ( $str =~ /[^\S\r\n]*(?:\r?\n)(\d\d\/\d\d)[^\S\r\n]+(\d\d:\d\d)[^\S\r\n]*(?:\r?\n)(.+)(?:\r?\n)[^\S\r\n]*(?:\r?\n)(.+)(?:\r?\n)(?:-|(\d):(\d)[^\S\r\n]+\(\d:\d\))[^\S\r\n]*(?:\r?\n)(.+)/g )
{
print "\n";
print "Date: $1\n";
print "Time: $2\n";
print "Place: $3\n";
print "Home: $4\n";
print "Away: $7\n";
print "Score: ";
if ( defined $5 ) {
print "$5 to $6\n";
}
else {
print "no info\n";
}
}
__DATA__
1
15/06 16:00
Brasília
Brasilien
3:0 (1:0)
Japan
2
23/06 16:00
Recife
Uruguay
-
Tahiti
但是我需要这些小组来获取数据@rubiktubik-给你。现在它工作了!非常感谢。但我不明白为什么这是工作和更简单的正则表达式不!?
var pattern = /[^\S\r\n]*(?:\r?\n)(\d\d\/\d\d)[^\S\r\n]+(\d\d:\d\d)[^\S\r\n]*(?:\r?\n)(.+)(?:\r?\n)[^\S\r\n]*(?:\r?\n)(.+)(?:\r?\n)(?:-|(\d):(\d)[^\S\r\n]+\(\d:\d\))[^\S\r\n]*(?:\r?\n)(.+)/g;
while ((match = pattern.exec( data )) != null)
{
console.log( "\n" );
console.log( "Date: " + match[1] + "\n";
console.log( "Time: " + match[2] + "\n";
console.log( "Place: " + match[3] + "\n";
console.log( "Home: " + match[4] + "\n";
console.log( "Away: " + match[7] + "\n";
console.log( "Score: ";
if (match[5] != null) {
console.log( match[5] + " to " + match[6] + "\n";
}
else {
console.log( "no info\n";
}
}
$/ = undef;
$str = <DATA>;
while ( $str =~ /[^\S\r\n]*(?:\r?\n)(\d\d\/\d\d)[^\S\r\n]+(\d\d:\d\d)[^\S\r\n]*(?:\r?\n)(.+)(?:\r?\n)[^\S\r\n]*(?:\r?\n)(.+)(?:\r?\n)(?:-|(\d):(\d)[^\S\r\n]+\(\d:\d\))[^\S\r\n]*(?:\r?\n)(.+)/g )
{
print "\n";
print "Date: $1\n";
print "Time: $2\n";
print "Place: $3\n";
print "Home: $4\n";
print "Away: $7\n";
print "Score: ";
if ( defined $5 ) {
print "$5 to $6\n";
}
else {
print "no info\n";
}
}
__DATA__
1
15/06 16:00
Brasília
Brasilien
3:0 (1:0)
Japan
2
23/06 16:00
Recife
Uruguay
-
Tahiti
Date: 15/06
Time: 16:00
Place: Brasflia
Home: Brasilien
Away: Japan
Score: 3 to 0
Date: 23/06
Time: 16:00
Place: Recife
Home: Uruguay
Away: Tahiti
Score: no info