mIRC Home    About    Download    Register    News    Help

Print Thread
#209144 05/02/09 02:19 AM
Joined: Nov 2008
Posts: 22
F
Ameglian cow
OP Offline
Ameglian cow
F
Joined: Nov 2008
Posts: 22
To start off, here's the regex:
Btw this is part of a while loop that loops through a .txt (hence the %i)

Code:
$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:

Code:
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?

Firstmate #209149 05/02/09 04:35 AM
Joined: Aug 2004
Posts: 7,252
R
Hoopy frood
Offline
Hoopy frood
R
Joined: Aug 2004
Posts: 7,252
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-


RusselB #209151 05/02/09 05:03 AM
Joined: Nov 2008
Posts: 22
F
Ameglian cow
OP Offline
Ameglian cow
F
Joined: Nov 2008
Posts: 22
I need it to replace it with the Second! part of the line (separated by a space). So I use 2-.

Edit:

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!

Last edited by Firstmate; 05/02/09 05:05 AM.
Firstmate #209152 05/02/09 05:32 AM
Joined: Oct 2005
Posts: 1,741
G
Hoopy frood
Offline
Hoopy frood
G
Joined: Oct 2005
Posts: 1,741
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:

Code:

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



-genius_at_work

Joined: Nov 2008
Posts: 22
F
Ameglian cow
OP Offline
Ameglian cow
F
Joined: Nov 2008
Posts: 22
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

Firstmate #209155 05/02/09 07:21 AM
Joined: Oct 2003
Posts: 3,918
A
Hoopy frood
Offline
Hoopy frood
A
Joined: Oct 2003
Posts: 3,918
A simple test example hints that you may have copied the code incorrectly:

Code:
//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?


- argv[0] on EFnet #mIRC
- "Life is a pointer to an integer without a cast"
argv0 #209172 05/02/09 08:41 PM
Joined: Nov 2008
Posts: 22
F
Ameglian cow
OP Offline
Ameglian cow
F
Joined: Nov 2008
Posts: 22
Here's the full code:

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)
      $regsub(%a,%search,%replace,%a)
      inc %i
    }
    echo -a %a
  } 
}

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

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

Last edited by Firstmate; 05/02/09 08:48 PM.

Link Copied to Clipboard