|
Joined: Mar 2005
Posts: 39
Ameglian cow
|
OP
Ameglian cow
Joined: Mar 2005
Posts: 39 |
Hello, I need a small regex for grabing the part between "posted" and "ago" in a phrase.. "Blehblehbleh got posted 16d 5h 42m 12s ago by blehbleh.." Si basiclly i need to grab the text in bold. ![smile smile](/images/graemlins/mirc/smile.gif)
|
|
|
|
Joined: Dec 2002
Posts: 3,547
Hoopy frood
|
Hoopy frood
Joined: Dec 2002
Posts: 3,547 |
Ok, well firstly I don't know anything about $regex() but if the duration is really in bold you could so something like:
//echo -a $gettok("Blehblehbleh got posted 16d 5h 42m 12s ago by blehbleh..",2,2)
-Andy
|
|
|
|
Joined: Mar 2005
Posts: 39
Ameglian cow
|
OP
Ameglian cow
Joined: Mar 2005
Posts: 39 |
Yep, thought about that , but all text are in same codes (bold 'n black)..
|
|
|
|
Joined: Dec 2002
Posts: 3,547
Hoopy frood
|
Hoopy frood
Joined: Dec 2002
Posts: 3,547 |
Hey dude, you could try to do this with a combination of Token Identifiers. You could try this until someone comes up with a $regex() pattern, but this seems to work for me. I've put it in a little alias to show you whats happening.
alias post {
var %string = Blehblehbleh got posted 16d 5h 42m 12s ago by blehbleh..
echo -a $gettok($gettok(%string,$+($findtok(%string,posted,1,32),-,$findtok(%string,ago,1,32)),32),$+(2,-,$calc($numtok($gettok(%string,$+($findtok(%string,posted,1,32),-,$findtok(%string,ago,1,32)),32),32) - 1)),32)
}
Obviously, %string holds the "Blehblehbleh got posted 16d 5h 42m 12s ago by blehbleh.." but this should give you an idea on how to manipulate it. Then just echoes it back basically. -Andy
|
|
|
|
Joined: Apr 2003
Posts: 701
Hoopy frood
|
Hoopy frood
Joined: Apr 2003
Posts: 701 |
var %blah = $regex($1-,/got posted (.*?) ago by/iS) This assumes $1- has the text line, strips color codes and ignores case (iS) and gives the time you wanted in $regml(1), until you do another $regex or $regsub. The var %blah is just used to get rid of the output of $regex, you can check if it actually matched or not, but then you better use if ($regex(..)) { ... } immediately ![smile smile](/images/graemlins/mirc/smile.gif) if you want all numbers separately: var %blah = $regex($1-,/got posted (\d+d |)(\d+h |)(\d+m |)(\d+s |)ago by/iS) where $regml(1) has number of days or is $null if none, $regml(2) is hours, ... $regml(4) is seconds or $null if not present
|
|
|
|
Joined: Mar 2005
Posts: 39
Ameglian cow
|
OP
Ameglian cow
Joined: Mar 2005
Posts: 39 |
Ah, that actually worked superb, Kelder. ![smile smile](/images/graemlins/mirc/smile.gif) Another short question; How do i grab the fourth last word in a sentence? [ bapplander / today ] (what i wanna grab is marked in bold, and the phrase is at the end of a full sentence.. ![smile smile](/images/graemlins/mirc/smile.gif)
|
|
|
|
Joined: Sep 2003
Posts: 4,230
Hoopy frood
|
Hoopy frood
Joined: Sep 2003
Posts: 4,230 |
hey only 4 days to go! Not that its important here, as i see hes got a regex now but... I looked at your code, and have had to do the same thing myself (refrencing something twice, the gettok from posted to ago), I came up with a sneaky method of getting around that, although i think it could be done without this otherways, heres one way of reducing your code overhead, and processing time, for things like this. I incase the original $gettok(post to ago) inside a $iif( ,$v1), this then allows me to refrence it again as $v1, see the spaced out alignment of the two lines below.
echo -a $gettok( $gettok(%string,$+($findtok(%string,posted,1,32),-,$findtok(%string,ago,1,32)),32) ,$+(2,-,$calc($numtok($gettok(%string,$+($findtok(%string,posted,1,32),-,$findtok(%string,ago,1,32)),32),32) - 1)),32)
echo -a $gettok( $iif($gettok(%string,$+($findtok(%string,posted,1,32),-,$findtok(%string,ago,1,32)),32),$v1) ,$+(2,-,$calc($numtok($v1 ,32) - 1)),32)
|
|
|
|
Joined: Dec 2002
Posts: 3,547
Hoopy frood
|
Hoopy frood
Joined: Dec 2002
Posts: 3,547 |
Hehe very sneaky with $v1 to make ot shorter, I actually never thought of it. Yep 4 days, but it's almost midnight here in England, so theoretically it'll be 3 days, almost time to update profile.. ![grin grin](/images/graemlins/mirc/grin.gif) -Andy
|
|
|
|
Joined: Apr 2003
Posts: 701
Hoopy frood
|
Hoopy frood
Joined: Apr 2003
Posts: 701 |
It's somewhat unclear what you want (and consider a word), so here's some guesses
get 4th last word, word being stuff between spaces: $gettok($1-,-4,32) get something that is between "[ " and " / today ]": $regex($1-,/\[ (\w+?) \/ today \]/)) and the word is in $regml(1)
|
|
|
|
Joined: Jan 2003
Posts: 87
Babel fish
|
Babel fish
Joined: Jan 2003
Posts: 87 |
Blehblehbleh got posted 16d 5h 42m 12s ago by blehbleh
alias gettime {
if ($regex($strip($1-),/posted\s((?:\d+d\s)?(?:\d+h\s)?(?:\d+m\s)?(?:\d+s\s)?)\s?ago/)) { return $regml(1) }
}
Then just $gettime(input) well returned the parsed bit you wanted.
|
|
|
|
Joined: Dec 2002
Posts: 3,547
Hoopy frood
|
Hoopy frood
Joined: Dec 2002
Posts: 3,547 |
That's why I'd love to learn $regex() heh, it makes the code I did seem like I took the long route out, but I guess our codes did the same thing anyway, but yours is more smaller and don't take up a whole line, hehe.. ![laugh laugh](/images/graemlins/mirc/laugh.gif) -Andy
|
|
|
|
Joined: Mar 2005
Posts: 39
Ameglian cow
|
OP
Ameglian cow
Joined: Mar 2005
Posts: 39 |
alias gettime {
if ($regex($strip($1-),/posted\s((?:\d+d\s)?(?:\d+h\s)?(?:\d+m\s)?(?:\d+s\s)?)\s?ago/)) { return $regml(1) }
}
Then just $gettime(input) well returned the parsed bit you wanted. [/quote] Well, that actually seemed to work quite good! ![smile smile](/images/graemlins/mirc/smile.gif) However, i just discovered.. when the post is older than 24 hrs, the date is added to it aswell.. So then the correct string will be: Blehblehbleh got posted 16d 5h 42m 12s [11/05/2005] ago by blehblehIs it possible to modify the regex to fit to that? Get it to like skip or ignore [date] or something..? And also, i get "1 16d 5h 42m 12s" in the output, why do i get the first "1" character?
Last edited by bapplander; 27/05/05 10:36 PM.
|
|
|
|
Joined: Apr 2003
Posts: 701
Hoopy frood
|
Hoopy frood
Joined: Apr 2003
Posts: 701 |
alias gettime {
if ($regex($1-,/posted ((?:\d+d )?(?:\d+h )?(?:\d+m )?(?:\d+s)?) ?(?:\[[\d/]+?\] )?ago/S)) {
return $regml(1)
}
} This should match the [date] stuff and ignore it. Used the //S modifier instead of $strip, rest is about the same...
|
|
|
|
|