[SOLVED] Regular expression excluding spaces

Just starting out? Need help? Post your questions and find answers here.
boddhi
Enthusiast
Enthusiast
Posts: 524
Joined: Mon Nov 15, 2010 9:53 pm

[SOLVED] Regular expression excluding spaces

Post 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.
Last edited by boddhi on Thu Jul 18, 2024 12:05 am, edited 2 times in total.
If my English syntax and lexicon are incorrect, please bear with Google translate and DeepL. They rarely agree with each other!
Except on this sentence...
AZJIO
Addict
Addict
Posts: 2225
Joined: Sun May 14, 2017 1:48 am

Re: Regular expression excluding spaces

Post 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
boddhi
Enthusiast
Enthusiast
Posts: 524
Joined: Mon Nov 15, 2010 9:53 pm

Re: Regular expression excluding spaces

Post 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:
If my English syntax and lexicon are incorrect, please bear with Google translate and DeepL. They rarely agree with each other!
Except on this sentence...
AZJIO
Addict
Addict
Posts: 2225
Joined: Sun May 14, 2017 1:48 am

Re: Regular expression excluding spaces

Post by AZJIO »

#PB_RegularExpression_Extended
" ( \S ) + " - these spaces will be excluded. Used to separate parts of code with spaces
Post Reply