mIRC Homepage
Posted By: Firstmate Fixing regex - 05/02/09 02:19 AM
To start off, here's the regex:
Btw this is part of a while loop that loops through a .txt (hence the %i)

$regsub(%a,/\b $gettok($read(hs.txt,%i),1,32)\b/g,$chr(32) $gettok($read(hs.txt,%i),2-,32),%a)

Now, I have a .txt, which has lines similar to the following:

Line1 Blah Blah Blah
24 123 123 123 

The regex replaces Line1 with Blah Blah Blah (I use it as a command later).
So for example:
Hi there 24
When passed through the regex, comes out to be:
Hi there 123 123 123

The problem is if the word needed to be replace is the FIRST word. e.g.

Line1 Hi there 24 Line1
When passed through, comes out
Line1 Hi there 123 123 123 Blah Blah Blah

As you see it replaces other text, but NOT the first word. Any help?
Posted By: RusselB Re: Fixing regex - 05/02/09 04:35 AM
regex knowledge aside, a quick look at your code and your problem, leads me to suspect that you want 1- in the second $gettok rather than 2-

Posted By: Firstmate Re: Fixing regex - 05/02/09 05:03 AM
I need it to replace it with the Second! part of the line (separated by a space). So I use 2-.


The one I posted, only replaces it if its NOT the first word, and also the first instance of it D:

This is confusing me D:! Help!
Posted By: genius_at_work Re: Fixing regex - 05/02/09 05:32 AM
It looks like it is because of the space character that you have after the \b in your regex. There wouldn't be a space before the first word in the sentence.

Try this code:


var %search = /\b $+ $gettok($read(hs.txt,%i),1,32)\b/g
var %replace = $gettok($read(hs.txt,%i),2-,32)

Posted By: Firstmate Re: Fixing regex - 05/02/09 07:00 AM
It still only replaces the first instance.

e.g.(btw --> by the way):
I entered: btw the btw
I get: by the way the btw
Posted By: argv0 Re: Fixing regex - 05/02/09 07:21 AM
A simple test example hints that you may have copied the code incorrectly:

//echo -a $regsubex(btw the btw, /\bbtw\b/g, by the way)

returns: by the way the by the way

Are you sure you included the //g switch?
Posted By: Firstmate Re: Fixing regex - 05/02/09 08:41 PM
Here's the full code:

alias hs.replace { 
  var %a $1-
  var %i = 1, %x = $calc($lines(hs.txt) + 1)
  if (%x == 1) { halt }
  else {
    while (%i < %x) {
      var %search = /\b $+ $gettok($read(hs.txt,%i),1,32)\b/g
      var %replace = $gettok($read(hs.txt,%i),2-,32)
      inc %i
    echo -a %a

Fixed it: It wasn't combining the \g in the script ^^, so I changed it to:

var %search = $+(/\b,$gettok($read(hs.txt,%i),1,32),\b/g)
© mIRC Discussion Forums