简体   繁体   English

正则表达式在两个字符串之间获取一个字符串

[英]regular expression get a string between two strings

I have this string : 我有这个字符串:

# userid name uniqueid connected ping loss state rate
# 529 2 "arioch83" STEAM_1:0:86796179 55:58 99 0 active 80000
# 619 3 "Snake" STEAM_1:0:27678629 06:42 61 0 active 80000
# 622 4 "Captain_Selleri" STEAM_1:1:47927314 03:25 44 0 active 80000
# 583 5 "krN[786]" STEAM_1:1:14638235 28:53 53 0 active 128000
# 621 6 "Giack" STEAM_1:1:67468100 04:44 129 0 active 80000
# 326 7 "Urkrass" STEAM_1:0:55150382  3:02:31 51 0 active 80000
#613 "Vinny" BOT active
# 584 9 "Tkappa" STEAM_1:0:32266787 27:55 360 0 active 80000
# 605 10 "Narpok19" STEAM_1:0:44838130 14:36 67 0 active 80000
# 551 11 "robbetto83" STEAM_1:1:63675894 50:10 86 0 active 80000
# 530 12 "XxKazuyaxX" STEAM_1:0:18676379 55:57 68 0 active 80000
# 623 13 "beut3d - Keyser Söze" STEAM_1:0:14500718 00:29 70 0 active 80000
# 602 15 "Homroy" STEAM_1:1:7870901 16:34 169 0 active 80000
# 607 16 "[Bloody]Eagle" STEAM_1:1:59567346 09:14 77 0 active 80000
#615 "Jeff" BOT active
# 587 18 "Heisenberg" STEAM_1:1:61427218 25:15 81 0 active 80000
#end

And I want to get string between "# userid name uniqueid connected ping loss state rate" and "#end". 我想在“#用户ID名称唯一ID连接ping丢失状态速率”和“ #end”之间获取字符串。 I tryied several regex based on what I found on stackoverflow but none works :( Here is my last attempt 我根据在stackoverflow上找到的内容尝试了几种正则表达式,但没有用:(这是我最后的尝试

var matches = text.match(/# userid name uniqueid connected ping loss state rate(.+)\&#end/);
                if (matches.length > 1) {
                    alert(matches[1]);
                }

As you can see I know nothing about regex... any good tuto about those ? 如您所见,我对正则表达式一无所知...关于它们的任何优秀教程?

thanks, 谢谢,

You need to be able to match across multiple lines. 您需要能够跨多行进行匹配。 You are looking for the s ( dotall ) modifier. 您正在寻找sdotall )修饰符。 Unfortunately this modifier does not exist in JavaScript. 不幸的是,此修饰符在JavaScript 中不存在。 A common workaround is replacing the dot . 常见的解决方法是替换点. with the following: 具有以下内容:

[\S\s]    

So you can use the following regular expression: 因此,您可以使用以下正则表达式:

/# userid name uniqueid connected ping loss state rate([\S\s]*?)#end/

Explanation : 说明

(               # group and capture to \1:
  [\S\s]*?      #   any character of: 
                #    non-whitespace (all but \n, \r, \t, \f, and " "), 
                #    whitespace (\n, \r, \t, \f, and " ") 
                #    (0 or more times matching the least amount possible)
)               # end of \1

JavaScript Demo JavaScript示范

An alternative to using regular expressions, is to split the string by \\n and extract the part that you want (everything from the second line to the second to last line): 使用正则表达式的一种替代方法是将字符串除以\\n并提取所需的部分(从第二行到第二行再到最后一行):

var contents = "your contents here...";

var contentArray = contents.split( "\n" );

// extract the array elements from the second to the second from last and re-join with `\n`
console.log( contentArray.slice( 1, contentArray.length - 2 ).join( "\n" ) );

Get all the lines that is not started by #userid and #end 获取所有未由#userid#end #userid的行

/^(?!#\s*(userid|end)).*/gmi

Online demo 在线演示

You can use this pattern that uses the fact that "#end" is at the start of a line: 您可以使用以下模式,该模式使用“ #end”位于行首的事实:

/# userid name uniqueid connected ping loss state rate\n((?:.*\n)*?)#end/

But an other efficient way is to split the log into lines and to look at the start and end lines in the resulting list. 但是另一种有效的方法是将日志分成几行,并查看结果列表中的开始和结束行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM