mIRC Home    About    Download    Register    News    Help

Print Thread
#168717 12/01/07 11:04 PM
Joined: Apr 2006
Posts: 464
O
Fjord artisan
OP Offline
Fjord artisan
O
Joined: Apr 2006
Posts: 464
Hi,

I'm looking for an Regex instruction that can find the following pattern in a string:

00:00:00

- The 0 represents a digit from 0 to 9
- The two : signs will always be there
- Pattern will be present multiple (2) times in the same string
- Pattern will be surrounded by spaces or by horizontal tabs
- The string will contain all kinds of characters, the pattern however ALWAYS be the numbers seperated with the 2 colons.
- Only 1 of the 2 digits will always be there, the second is optional.

So it should match patterns like:
0:0:00
0:00:00
00:00:00
00:00:0
00:0:0

Op top of that, this pattern will always be present twice. So, I actually need 2 Regex instructions.
One to find the first instance, and one for the second.

I hope the query itself is clear.
If anyone knows how this can be done, please let me know!

Thanks a lot in advance.

OrionsBelt #168719 12/01/07 11:59 PM
Joined: Jan 2007
Posts: 259
K
Fjord artisan
Offline
Fjord artisan
K
Joined: Jan 2007
Posts: 259
Code:
/^(.*)([0-9][0-9]?):([0-9][0-9]?):([0-9][0-9]?)(.*)([0-9][0-9]?):([0-9][0-9]?):([0-9][0-9]?)(.*)$/

Should work for you, $regml(1) being before the first match, $regml(2-4) being the results in the first xx:xx:xx (excluding the : :), $regml(5) being the inbetween, $regml(6-8) being the next xx:xx:xx match, and $regml(9) being the rest after the last match untill the end.

Example:
Code:
; %s must be the string.
if ($regex(%s,/^(.*)([0-9][0-9]?):([0-9][0-9]?):([0-9][0-9]?)(.*)([0-9][0-9]?):([0-9][0-9]?):([0-9][0-9]?)(.*)$/)) {
var %m.1 = $regml(1), %m.2 = $regml(2),%m.3 = $regml(3), %m.4 = $regml(4),%,.1 = $regml(5), %m62 = $regml(6),%m.7 = $regml(7), %m.8 = $regml(8),%m.9 = $regml(9), %m.2-4 = $+(%m.2,:,%m.3,:,%m.4), %m.6-8 = $+(%m.6,:,%m.7,:,%m.8)
}
; You can now use %m.(num) to retrive a result, 1 being before, etc as described above. I also made %m.2-4 and %m.6-8 to the whole first, and whole second.

Last edited by Kardafol; 13/01/07 12:05 AM.

Those who can, cannot. Those who cannot, can.
Kardafol #168723 13/01/07 01:26 AM
Joined: Apr 2006
Posts: 464
O
Fjord artisan
OP Offline
Fjord artisan
O
Joined: Apr 2006
Posts: 464
Wow dude, thats pretty amazing.
I tested it a bit, and its almost working.

Bit of a re-write of your code (for me to understand):
Code:
on *:TEXT:*:#channel:{
  var %string = $1-
  if ($regex(%string,/^(.*)([0-9][0-9]?):([0-9][0-9]?):([0-9][0-9]?)(.*)([0-9][0-9]?):([0-9][0-9]?):([0-9][0-9]?)(.*)$/)) {
    var %m.1 = $regml(1)
    var %m.2 = $regml(2)
    var %m.3 = $regml(3)
    var %m.4 = $regml(4)
    var %m.5 = $regml(5)
    var %m.6 = $regml(6)
    var %m.7 = $regml(7)
    var %m.8 = $regml(8)
    var %m.9 = $regml(9)

    var %m.2-4 = $+(%m.2,:,%m.3,:,%m.4)
    var %m.6-8 = $+(%m.6,:,%m.7,:,%m.8)

    msg $chan 1st set is %m.2-4
    msg $chan 2nd set is %m.6-8
  }
}


At the moment, the problem is that it won't recognise a double digit for the first part of the pattern, so the %m.2 and %m.6 part I guess?

00:00:00 returns 0:00:00 and should return 00:00:00
00:0:0 returns 0:0:0 and should return 00:0:0
00:00:0 returns 0:00:0 and should return 00:00:0

Any idea's on how to fix this??

Thanks for helping man, really appreciated.

OrionsBelt #168725 13/01/07 01:42 AM
Joined: Apr 2006
Posts: 464
O
Fjord artisan
OP Offline
Fjord artisan
O
Joined: Apr 2006
Posts: 464
if ($regex(%string,/^(.*)\s([0-9][0-9]?):([0-9][0-9]?):([0-9][0-9]?)(.*)\s([0-9][0-9]?):([0-9][0-9]?):([0-9][0-9]?)(.*)$/)) {

I think that \s switch did it.
Is that correct? Or should it be done on some other way?

Thank you again smile

OrionsBelt #168726 13/01/07 01:46 AM
Joined: Jan 2007
Posts: 259
K
Fjord artisan
Offline
Fjord artisan
K
Joined: Jan 2007
Posts: 259
That would break the regex. I'm thinking of a solution. (bla bla <TAB>00 <-- would not be recognized)
I can come to your IRC network, since it could speed up things.

Last edited by Kardafol; 13/01/07 01:49 AM.

Those who can, cannot. Those who cannot, can.
OrionsBelt #168727 13/01/07 01:51 AM
Joined: Jan 2007
Posts: 259
K
Fjord artisan
Offline
Fjord artisan
K
Joined: Jan 2007
Posts: 259
Code:
...
if ($regex(%string,/^(.*)([0-9][0-9]?+):([0-9][0-9]?):([0-9][0-9]?)(.*)([0-9][0-9]?+):([0-9][0-9]?):([0-9][0-9]?)(.*)$/)) {

That should do it. the + after the ? means its "Greedy", meaning it will try to fill that spot first.
The regex alone:
Code:
/^(.*)([0-9][0-9]?+):([0-9][0-9]?):([0-9][0-9]?)(.*)([0-9][0-9]?+):([0-9][0-9]?):([0-9][0-9]?)(.*)$/

Last edited by Kardafol; 13/01/07 01:52 AM.

Those who can, cannot. Those who cannot, can.
Kardafol #168728 13/01/07 01:54 AM
Joined: Apr 2006
Posts: 464
O
Fjord artisan
OP Offline
Fjord artisan
O
Joined: Apr 2006
Posts: 464
Nope, didn't work.

I'll send ya a pm.

Kardafol #168729 13/01/07 02:46 AM
Joined: Apr 2006
Posts: 464
O
Fjord artisan
OP Offline
Fjord artisan
O
Joined: Apr 2006
Posts: 464
Sorted grin

Thx a lot Kardafol

OrionsBelt #168730 13/01/07 03:04 AM
Joined: Oct 2005
Posts: 1,741
G
Hoopy frood
Offline
Hoopy frood
G
Joined: Oct 2005
Posts: 1,741
Try this:

Code:
alias retest1 {
  var %s = beginning text 00:0:00 middle text 00:00:00 end text
  if ($regex(re1,%s,/^((?:.+\s+)?)((\d\d?):(?3):(?3))\s+((?:.+\s+)?)((?2))((?:\s+.+)?)$/i)) {

    echo -a (Before): $regml(re1,1)
    echo -a (Match1): $regml(re1,2) 
    echo -a (Middle): $regml(re1,4) 
    echo -a (Match2): $regml(re1,5) 
    echo -a (Ending): $regml(re1,6)
  }
  else { echo -a No Match }
}


The $regml's are shown in the code above. Note that $regml #3 is used to contain an expression that is repeated, so it doesn't contain any useful data.

I wasn't sure whether you wanted to match 2 different or identical strings (strings, not patterns).

The code above matches 2 different strings in 2 places with the same pattern.
Example: "TEXT 00:0:00 TEXT 00:0:00 TEXT" = match
Example: "TEXT 0:00:0 TEXT 00:0:00 TEXT" = match


If you want to match the same string in 2 places, change the (?2) to (\2) in the code above.
Example: "TEXT 00:0:00 TEXT 00:0:00 TEXT" = match
Example: "TEXT 0:00:0 TEXT 00:0:00 TEXT" = NO match

-genius_at_work

Joined: Oct 2006
Posts: 166
B
Vogon poet
Offline
Vogon poet
B
Joined: Oct 2006
Posts: 166
Just a note that you can group your nn:nn:nn at make length match for it. as I did below.
Code:
alias retest1 {
  var %s = beginning text 00:0:00 middle text 00:00:00 end text
  if ($regex(re1,%s,/(?:((\d\d?)((?1)|\:|\s|){3})|([a-z].*?)(?=\s0|$))/gi)) {
    echo -a (Before): $regml(re1,1)
    echo -a (Match1): $regml(re1,2) 
    echo -a (Middle): $regml(re1,5) 
    echo -a (Match2): $regml(re1,6) 
    echo -a (Ending): $regml(re1,9)
  }
  else { echo -a No Match }
}

/(?:((\d\d?)((?1)|\:|\s|){3})|([a-z].*?)(?=\s0|$))/gi


Kind Regards, blink
b1ink #168748 13/01/07 05:53 PM
Joined: Oct 2005
Posts: 1,741
G
Hoopy frood
Offline
Hoopy frood
G
Joined: Oct 2005
Posts: 1,741
Sorry, but your code doesn't work as expected. At least, not how you have it written.

Code:
alias retest2 {
  var %s = $1-
  if ($regex(re1,%s,/(?:((\d\d?)((?1)|\:|\s|){3})|([a-z].*?)(?=\s0|$))/gi)) {
    echo -a (Before): $len($regml(re1,1)) $regml(re1,1)
    echo -a (Match1): $len($regml(re1,2)) $regml(re1,2)
    echo -a (Middle): $len($regml(re1,5)) $regml(re1,5) 
    echo -a (Match2): $len($regml(re1,6)) $regml(re1,6) 
    echo -a (Ending): $len($regml(re1,9)) $regml(re1,9)
  }
  else { echo -a No Match }
}


Works: /retest2 beginning text 00:0:00 middle text 00:00:00 end text
Works: /retest2 beginning text 00:0:00 middle text 00:00:00
Fails: /retest2 beginning text 00:0:00 00:00:00 end text
Fails: /retest2 00:0:00 middle text 00:00:00 end text
Fails: /retest2 00:0:00 00:00:00
Fails: /retest2 beginning text 00:0:00 middle text end text
Fails: /retest2 beginning text middle text end text
Fails: /retest2 beginning text 000:0:00 middle text 00:00:00 end text
Fails: /retest2 beginning text 00:0:000 middle text 00:00:00 end text

(Note: "fails" means the results aren't what the OP requested, or can use reliably.)

Since the match is this:
<optional text> <pattern> <optional text> <pattern> <optional text>
I tried using another group to repeat the [<optional text> <pattern>] part, but the $regml values weren't populated properly.

-genius_at_work

Joined: Oct 2006
Posts: 166
B
Vogon poet
Offline
Vogon poet
B
Joined: Oct 2006
Posts: 166
That can be done easily but I thought to shorten the pattern.


Kind Regards, blink
b1ink #168757 13/01/07 09:44 PM
Joined: Jan 2007
Posts: 259
K
Fjord artisan
Offline
Fjord artisan
K
Joined: Jan 2007
Posts: 259
It dosen't really matter, as we have already resolved the problem.


Those who can, cannot. Those who cannot, can.

Link Copied to Clipboard