2

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". I tryied several regex based on what I found on stackoverflow but none works :( Here is my last attempt

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,

2
  • For what your are looking for? Please explain a bit more. Commented Jul 11, 2014 at 23:50
  • 1
    a toy, a guide, a clue Commented Jul 11, 2014 at 23:50

4 Answers 4

5

You need to be able to match across multiple lines. You are looking for the s (dotall) modifier. Unfortunately this modifier does not exist in 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

Sign up to request clarification or add additional context in comments.

4 Comments

we can use [\W\w]* and [\D\d]* as well. Nice answer.
You should be able to use [^]* too.
There are many ways, yes, as I stated A common workaround...
@hwnd: if I put "groink#end" after the example string, your pattern will match it, you need to use a lazy quantifier.
0

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):

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" ) );

Comments

0

Get all the lines that is not started by #userid and #end

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

Online demo

Comments

0

You can use this pattern that uses the fact that "#end" is at the start of a line:

/# 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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.