mIRC Homepage

Need help with regular expressions

Posted By: klez

Need help with regular expressions - 21/04/21 10:24 AM

Hi!

Please if someone can help me... i don't know well Regular Expression and i want to use a regular expresssion on this example:

I have 2 strings delimitated by comma:

1) a,b,c,d,e,f,g,h,$chr(44),$chr(46),i,j,k i.e. %string1 = a,b,c,d,e,f,g,h,$chr(44),$chr(46),i,j,k
2) i,j,k,l,m,n,o,p,r,s,t,a,$chr(44),u i.e. %string2 = i,j,k,l,m,n,o,p,r,s,t,a,$chr(44),u

So i want a regular expression that will tell me if any symbol from the first %string1 is containing in the second %string2 or not. I want to let you know that could be any symbol: letter, number etc.)

Thank you in advance.
Posted By: Wims

Re: Need help with regular expressions - 21/04/21 12:40 PM

Quote
//var -s %string1 = a,b,c,d,e,f,g,h,$chr(44),$chr(46),i,j,k ,%string2 i,j,k,l,m,n,o,p,r,s,t,a,$chr(44),u | if ($true isin $regsubex(%string1,/([^\x2c]+)/g,$istok(%string2,\1,44))) echo -ag yes | else echo -ag no


A better way:

Quote
//var %string1 a,b,c,d,e,f,g,h,$chr(44),$chr(46),i,j,k ,%string2 i,j,k,l,m,n,o,p,r,s,t,a,$chr(44),u | if ($regex(%string1 %string2,/([^\x2c ]++)(?=[^ ]* (?:[^\x2c ]++\x2c)*?\1)/)) echo -ag yes $regml(1)
Posted By: klez

Re: Need help with regular expressions - 22/04/21 03:54 PM

Wims, thank you very much for you help.. but if you can help me with 2 issues also, would be great and thank you one more time:

The first issue:

1. if the %string1 = this is a line text with all symbols, letters and numbers!

2. %string2 = a,b,c,d,e,$chr(44),f,g

All the symbols from %string2 is delimitated by comma, without any space. So what's the regular expression that will return $true if all the symbols from %string1 are tokens of %string2 and $false if, at least, a symbol from %string1 is not a token of %string2?

For example, if %string1 = exampletext and %string2 = a,b,c,e,l,m,n,p,x,t so the regular expression will return $true because any char from %string1 is containing in %string2 and if %string1 = Exampletexts, %string2 = a,b,c,e,l,m,n,p,x,t will return $false because s and E are not containing in the %string2. I want to mention that is case-sensitive, i.e. E and e are different.

The second issue:

If i have a text line:

%line = th!is is a !simple !text

If you can help me with a regular expression that will return all the positions of the words (that are separated by space only) that begins with symbol ! (instead of ! also i can change with ? or . or another symbol) and will exclude those words if that symbol are inside of the words or at the end. The position is not a char position, but is the position of the word delimitated by space (i.e. $findtok(%line,!text,1,32) = 5)

Thank you very much for your help.
Posted By: Raccoon

Re: Need help with regular expressions - 23/04/21 11:49 AM

I'd recommend a /while loop with $istok(), but it should be possible to merge the two strings together and create a pattern that walks for matching tokens on both sides of the center line.

//var %s = f,o,x,e,s|p,i,x,e,l,s | echo -a $regex(%s,/(?<=^|,)([^|,]+)(?=(?:,.*?)?\|(?:.*?,)?\1(?:,|$))/g) - $regml(1) - $regml(2) - $regml(3) - $regml(4) - $regml(5)

> 3 - x - e - s - -

This might contain some bugs that I missed, but I think I caught them all.

Code
/(?<=^|,)([^|,]+)(?=(?:,.*?)?\|(?:.*?,)?\1(?:,|$))/g

/
  (?<=^|,)      // look-behind for either start-of-line or a comma.
  ([^|,]+)      // capture group: start reading characters that are neither pipe nor comma.
  (?=           // look-ahead: and these characters must be followed by all of this...
    (?:,.*?)?   //   optionally: a comma and more characters on the left side of the pipe
    \|          //   the center-line pipe must be there
    (?:.*?,)?   //   optionally: more characters followed by a comma
    \1          //   the matching capture group of characters from earlier
    (?:,|$)     //   must be followed by a comma or the end-of-line
  )             // end of look-ahead collection.
/g              // global:  do this over and over again to find multiple matches.


If I were to write this a little better, I would use the magical \G anchor at the beginning to assert that nothing gets skipped, but I already wrote it so.
© 2021 mIRC Discussion Forums