Page 1 of 1

[SOLVED] Regular expression excluding spaces

Posted: Wed Jul 17, 2024 11:23 pm
by boddhi
Hello,

It's late at night where I live, and maybe I no longer have eyes facing the holes, but I don't understand the results returned by PB's RegEx instructions.

Normally I'd only get "words" that are separated by spaces, but that's not the case. Spaces are returned too as an empty string!???
There must be something that escapes my reasoning...

Code: Select all

String.s="This is example of string that contains spaces"

;If CreateRegularExpression(0,"[^"+Chr(32)+Chr(255)+"]*") ; Chr(32)+Chr(255)="  " : Doesn't works under PB too!???
If CreateRegularExpression(0,"(\S)*") ; \S = any non white-space character
  If ExamineRegularExpression(0,String)
    While NextRegularExpressionMatch(0)
      Debug "Position: "+RegularExpressionMatchPosition(0)+" "+"String: |"+RegularExpressionMatchString(0)+"|"
    Wend
  EndIf
  FreeRegularExpression(0)
EndIf

Debug "*******"
Debug "*******"

Debug "Example with #PB_RegularExpression_Extended"
String="This is another example of string that contains spaces"

If CreateRegularExpression(0,"(\S)*",#PB_RegularExpression_Extended)
  If ExamineRegularExpression(0,String)
    While NextRegularExpressionMatch(0)
      Debug "  Position: "+RegularExpressionMatchPosition(0)+" "+"String: |"+RegularExpressionMatchString(0)+"|"
    Wend
  EndIf
  FreeRegularExpression(0)
EndIf
DebugOutput window wrote: Position: 1 String: |This|
Position: 5 String: ||
Position: 6 String: |is|
Position: 8 String: ||
Position: 9 String: |example|
Position: 16 String: ||
Position: 17 String: |of|
Position: 19 String: ||
Position: 20 String: |string|
Position: 26 String: ||
Position: 27 String: |that|
Position: 31 String: ||
Position: 32 String: |contains|
Position: 40 String: ||
Position: 41 String: |spaces|
*******
*******
Example with #PB_RegularExpression_Extended
Position: 1 String: |This|
Position: 5 String: ||
Position: 6 String: |is|
Position: 8 String: ||
Position: 9 String: |example|
Position: 16 String: ||
Position: 17 String: |of|
Position: 19 String: ||
Position: 20 String: |string|
Position: 26 String: ||
Position: 27 String: |that|
Position: 31 String: ||
Position: 32 String: |contains|
Position: 40 String: ||
Position: 41 String: |spaces|
Thanks for your ligths.

Re: Regular expression excluding spaces

Posted: Wed Jul 17, 2024 11:32 pm
by AZJIO
"(\S)+"
"\S+"

Code: Select all

EnableExplicit
#re = 0
Define String.s="This is example of string that contains spaces"
Define Dim Arr$(0)
Define i, NbFound

If CreateRegularExpression(#re, "\S+", 0)
	NbFound = ExtractRegularExpression(#re, String, Arr$())
Else
	Debug RegularExpressionError()
EndIf

; Debug  NbFound
For i = 0 To NbFound - 1
	Debug Arr$(i)
Next

Re: Regular expression excluding spaces

Posted: Wed Jul 17, 2024 11:47 pm
by boddhi
AZJIO wrote: "(\S)+"
:shock: F***!!! I haven't enough harsh words about myself!!!! :mrgreen: I confused the two symbols.
I think I really need to go to sleep.

I persisted, especially since I had validated my RegEX through RegEx101.com.
However, I don't understand why it doesn't work with #PB_RegularExpression_Extended which normally excludes spaces..


Thanks AZJIO :wink:

Re: Regular expression excluding spaces

Posted: Wed Jul 17, 2024 11:50 pm
by AZJIO
#PB_RegularExpression_Extended
" ( \S ) + " - these spaces will be excluded. Used to separate parts of code with spaces