Making PCRE compatible or similar to other RegExps

Post

Posted
Rating:
#1 (In Topic #1459)
Regular
sergioabreu is in the usergroup ‘Regular’
PCRE is nice but a little "peculiar".  

Their properties and methods behave different from Regexp in other famous languages. (PHP, javascript, etc)

I am not criticizing, just reporting facts and I offer a sugestion in the end that makes it compatible and a "real" findall method, you can call it allMatches method

RegExp.text is actually the "Match[0]" but is <COLOR color="#CC0000">not included</COLOR> in the Count value, making a bit confusing if the person didn't read the documentation carefully.
Checking Count=0 leads a beginner to think "Oh, there is no matches…" leaving the RegExp.Text that actually has Match[0]
 
findall can not be executed against  the full pattern with some paretheses groups. It only works if we provide "subgroup pattern" to it.
For example:

Code (gambas)

  1. RegExp.findall( "the whole text 1234", "[\\w\\ ]+(\\d+)", RegExp.Extended)
Will find only the whole string, skipping subgroup match. But if the pattern is only <COLOR color="#0000BF">\w+</COLOR> it finds 4 tokens.

I wrote this peace of code that makes it similar to other languages.
Using the example above, this Function will return the full source matched AND all its submatches, with a real Count= 2 that is the whole + the submatch

Code (gambas)

  1. Public Function allMatches(source As String, pattern As String, Optional regOptions As Integer = 11) As String[]
  2.  
  3.   Dim sx As New String[]
  4.   Dim reg As New RegExp(source, pattern, regOptions) '11 = extended + multiline + case insensitive
  5.  
  6.   If reg.text Then
  7.      sx.Add(reg.text) ' <= Match 0
  8.  
  9.   If reg.Count > 0 Then  '  <= Subgroup Matches 1, 2, etc...  
  10.     i = 1    
  11.     While i <= reg.Count
  12.       sx.Add(reg[i].text)
  13.       i += 1
  14.     Wend    
  15.   Endif  
  16.  
  17.   Return sx
  18.  
  19.  
  20.  

<COLOR color="#0000BF">That could be added in future versions</COLOR>, an <COLOR color="#800000">allMatches</COLOR> method, please show this to Benoir.
Online now: No Back to the top

Post

Posted
Rating:
#2
Avatar
Administrator
gbWilly is in the usergroup ‘unknown’

sergioabreu said

please show this to Benoir.
If you go to: https://lists.gambas-basic.org/ you will find the User list if you scroll down (see image).

Image

(Click to enlarge)


Subscribe and post your request there and your "please show this to Benoit." has magically happened. Who could have ever guessed that to be possible… ;)

gbWilly
- Gambas Dutch translator
- Gambas wiki content contributor
- Gambas debian/ubuntu package recipe contributor
- GambOS, a distro for learning Gambas and more…
- Gambas3 Debian/Ubuntu repositories


… there is always a Catch if things go wrong!
Online now: No Back to the top
1 guest and 0 members have just viewed this.