|
Joined: Sep 2003
Posts: 4,230
Hoopy frood
|
OP
Hoopy frood
Joined: Sep 2003
Posts: 4,230 |
ok here it is, i thought i would put it out to everyone to see what others come back with, since mine is just a big messy loop.
I want to be able to highlight using ctrl-r (reverse) a substring of a main string. The mainstring well have BURKO codes (ctrl-b,ctrl-u,ctrl-k) but NOT ctrl-r (these are already stripped out) in it so its any text with anything in it except for ctrl-r The substring well be present in the mainstring BUT well not include BURKO codes (its been stripped) I predict the likely hood of multiple occurances of the substring being present almost nill, first instance only need be worried about, unless you feel so inclined to locate everyone, if its no hardship.
I need an identifier that when passed the mainstring and the substring returns the mainstring with the start and end of the substring marked with ctrl-r
Error trapping is not needed for things like ctrl-r in the mainstring, a simple $remove well strip them out anyway, and checking if substring exists, since I can vouch for the contents of the two input values.
examples //echo $markup(this is an example string,s an exam) this is an example string
//echo $markup(8this is a11n example str7ing,s an exam) 8this is a11n example str7ing
//echo $markup(8this is a11n example str7ing,s an exam) 8this is a11n example str7ing
Background info : this is being used in an ON TEXT from channel chatter, the channel text is being stripped and certian key phrases (some of them dynamic based on other message content or nickname) are being scanned for in the text, if one is located, it is wanted to be highlighted in the original UNstripped text using reverse ctrl-r.
---------- ADVANCED POINT to look out for If u can do it, if not dont worry.... When a ctrl-o (ordinary) is in the mainstring this well halt the ctrl-r (reverse), if its possable to detect any ctrl-o's inside the substring area of the mainstring, it would be good to place a ctrl-r following them.
example //echo $markup(this is an example string,s an exam) this is an example string
likely hood of this is expected to be extremly low ----------
** im able to do this, have done infact, but its messy, it uses loops, and i think its prone to mistakes. **
Just wanting to see what others might come up with, see other peoples aproches to it, maybe some whizz has a regex solution even?
|
|
|
|
Joined: Oct 2005
Posts: 1,741
Hoopy frood
|
Hoopy frood
Joined: Oct 2005
Posts: 1,741 |
Here are three codes that i came up with using regex. All three codes should preserve the BUO codes. Color codes (K) that are outside the are being matched (the part of the output that is surrounded by the R codes) will be maintained. (Note: "haystack" is the string to search IN, and "needle" is the string to search FOR) ================================================= Code #1: This is the most versatile code. It will preserve all color codes with or without background colors. This code has a needle character limit of 29. alias markup1 return $iif($1,$regsubex($1,$+(/,$chr(40),$iif($2,$regsubex($2,/(.)/g,$+($replace(\t,$chr(32),\s),(?:[]|\d\d?(?:\x2c\d\d?)?)*)),^$),$chr(41),/gi),$+(,$replace(\1,,),))) ==================================================== Code #2: This code will not match a portion of a haystack that has color codes with background colors (ie. 2,10) in the match section (the ,10 is not recognized as part of the color code, which causes the match to fail). However, the code WILL match a portion of the haystack that has a foreground-only color code (ie. 2). Color codes that are NOT within the matched area of the haystack should work normally. This code has a needle character limit is 52. alias markup2 return $iif($1,$regsubex($1,$+(/,$chr(40),$iif($2,$regsubex($2,/(.)/g,$+($replace(\t,$chr(32),\s),(?:[]|\d\d?)*)),^$),$chr(41),/gi),$+(,$replace(\1,,),)))[code]
You can use the following code to remove the background while preserving the foreground.
$regsubex($1-,/(\d\d?)(?:\x2c\d\d?)?/g,\1)
=====================================================
Code #3:
This code will not match a portion of the haystack that has ANY color code, including foreground.
This code has a needle character limit of 134.
[code]alias markup3 return $iif($1,$regsubex($1,$+(/,$chr(40),$iif($2,$regsubex($2,/(.)/g,$+($replace(\t,$chr(32),\s),[]*)),^$),$chr(41),/gi),$+(,$replace(\1,,),))) To strip all the color codes, you can simply use: $strip($1-,k) ===================================================== When testing this, I used this format: //echo -a $markup(xy za bc xyz a bcx y z,a bc) Note that if you try to add color codes with background color (ie 2,10) into the above format, the comma will make the code not work (because it changes the contents of $1, $2, $3, etc). To get around this, put the code into an alias like this:
alias testmarkup {
var %haystack = thi2,10s is the string to sear2ch in
var %needle = g to search i
echo -a $markup1(%haystack,%needle)
}
When specifying the search term (needle), you should strip BURKO. It may behave strangely if the BURKO codes are left in. If there are any problems or questions, post them here. -genius_at_work
|
|
|
|
Joined: Sep 2003
Posts: 4,230
Hoopy frood
|
OP
Hoopy frood
Joined: Sep 2003
Posts: 4,230 |
This is the most versatile code. It will preserve all color codes with or without background colors. This code has a needle character limit of 29. alias markup1 return $iif($1,$regsubex($1,$+(/,$chr(40),$iif($2,$regsubex($2,/(.)/g,$+($replace(\t,$chr(32),\s),(?:[]|\d\d?(?:\x2c\d\d?)?)*)),^$),$chr(41),/gi),$+(,$replace(\1,,),))) This is excellent, i have tested it on a number of varations and it passed everytime, with a far FAR faster return time than mine (likely not that importatnt since it only goes off once per on text but faster IS faster!), for the 29 character limit, looking at some real world samples, about 80% of the needle strings are below that, and the ones above it , i simply cut the needle up into chunks and called it repeatedly using the output of the previous as the haystack, then removed and double ctrl-r's, this seemed to work fine, thankyou. PS: i assume the limit was due to string length, and that a regex match was needed per character of the needle, which accumulated in max 29 characters?
|
|
|
|
Joined: Jan 2003
Posts: 2,523
Hoopy frood
|
Hoopy frood
Joined: Jan 2003
Posts: 2,523 |
You can raise the limit from 29 to 70 with something like this: alias markup1 return $iif($1,$regsubex($1,$+(/,$chr(40),$iif($2,$+($left($2,1),[color:red]((?:[]|\d\d?(?:\x2c\d\d?)?)*)[/color],$regsubex($mid($2,2),/(.)/g,$+($replace(\t,$chr(32),\s),[color:red](?2)[/color]))),^$),$chr(41),/gi),$+(,$replace(\1,,),))) (?N) is like \N except it refers to the subpattern itself (and not the captured substring).
/.timerQ 1 0 echo /.timerQ 1 0 $timer(Q).com
|
|
|
|
Joined: Sep 2005
Posts: 2,881
Hoopy frood
|
Hoopy frood
Joined: Sep 2005
Posts: 2,881 |
(?N) is like \N except it refers to the subpattern itself (and not the captured substring). Could you rephrase that? I've wondered what the difference is for a while but I don't see what you mean.
|
|
|
|
Joined: Jan 2003
Posts: 2,523
Hoopy frood
|
Hoopy frood
Joined: Jan 2003
Posts: 2,523 |
The best way is by an example: //echo -a $regex(ab cd ef,/([a-z]+) \1 \1/) you can see why this fails: \1 is filled with the captured string from ([a-z]+), which is "ab". So this regex is trying to match whatever was first captured 3 times (ie "ab ab ab") which of course fails.
Now consider this: //echo -a $regex(ab cd ef,/([a-z]+) (?1) (?1)/g) this succeeds because (?1) is actually a shortcut for the first () subpattern in the regex. So the above pattern is equivalent to /([a-z]+) ([a-z]+) ([a-z]+)/ or more accurately to /([a-z]+) (?:[a-z]+) (?:[a-z]+)/ since (?N) themselves aren't capturing subpatterns.
In markup1, the second red part is a shortcut for the (much lengthier) first part. That second part is what's repeated, thus saving characters.
/.timerQ 1 0 echo /.timerQ 1 0 $timer(Q).com
|
|
|
|
Joined: Sep 2005
Posts: 2,881
Hoopy frood
|
Hoopy frood
Joined: Sep 2005
Posts: 2,881 |
Ah. Thanks a lot
|
|
|
|
Joined: Oct 2005
Posts: 1,741
Hoopy frood
|
Hoopy frood
Joined: Oct 2005
Posts: 1,741 |
I've never seen (?n) before. I thought there should be something like that in regex, but I didn't know what the syntax was. It looks pretty handy.
-genius_at_work
|
|
|
|
Joined: Sep 2003
Posts: 4,230
Hoopy frood
|
OP
Hoopy frood
Joined: Sep 2003
Posts: 4,230 |
I can see something up in the sky, but its like a little dot, i think becuase its gone so far over my head :-) Thanks for the upgrade as well
|
|
|
|
|