EscapeRegularExpression() function?

Just starting out? Need help? Post your questions and find answers here.
Quin
Addict
Addict
Posts: 1135
Joined: Thu Mar 31, 2022 7:03 pm
Location: Colorado, United States
Contact:

EscapeRegularExpression() function?

Post by Quin »

Hi,
In Python, there's a function in the re module, called escape. Essentially what it does is escape a character for use within a regular expression. For example "." would become "\.". Is there something like this for PB?
Thanks!
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4955
Joined: Sun Apr 12, 2009 6:27 am

Re: EscapeRegularExpression() function?

Post by RASHAD »

Maybe
String lib

Code: Select all

EscapeString(String$ [, Mode])
UnescapeString(String$ [, Mode])
Egypt my love
Quin
Addict
Addict
Posts: 1135
Joined: Thu Mar 31, 2022 7:03 pm
Location: Colorado, United States
Contact:

Re: EscapeRegularExpression() function?

Post by Quin »

I don't think that'll do it unfortunately, that only supports PB escapes, XML and JSON from what I can see, not regexes.
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4955
Joined: Sun Apr 12, 2009 6:27 am

Re: EscapeRegularExpression() function?

Post by RASHAD »

Sample for using Escape with RegularExpression
It may help

Code: Select all

Text$ = ~"<a href=\"https://www.sample.com\">Share</a>" + #CRLF$
Text$ + ~"<p> Share</p>" + #CRLF$
Text$ + ~"<a NoReplaceMe>Share</a>" + #CRLF$
Text$ + ~"<a href=\"https://www.sample.com\">Share</a>" + #CRLF$
Text$ + ~"<p> Share</p>" + #CRLF$
Text$ + ~"<p> Share</p>" + #CRLF$
Text$ + ~"<a href=\"https://www.sample.com\">Share</a>" + #CRLF$

Dim String$(0)
CreateRegularExpression(0, "(?<=<)a(?=\s+href)|(?<=</)a(?=>)", #PB_RegularExpression_NoCase)
For k = 1 To 100
  ReDim String$(k)
  String$(k) = StringField(Text$, k,#CRLF$)
  If String$(k) = ""
    Break
  ElseIf Left(String$(k),7)= "<a href"
    new$ = ReplaceRegularExpression(0, UnescapeString(string$(k)), "zz")
    String$(k) = EscapeString(new$)
  EndIf
  final$ = final$ + string$(k)+#CRLF$
Next
FreeRegularExpression(0)
Debug final$
Egypt my love
AZJIO
Addict
Addict
Posts: 2193
Joined: Sun May 14, 2017 1:48 am

Re: EscapeRegularExpression() function?

Post by AZJIO »

Code: Select all

; AZJIO
EnableExplicit

Procedure ListToString(*Result.string, List StrList.s())
	Protected Len, *Point

	ForEach StrList()
		Len + Len(StrList())
	Next

	*Result\s = Space(Len)
	*Point = @*Result\s
	ForEach StrList()
		CopyMemoryString(StrList(), @*Point)
	Next

EndProcedure


Procedure.s EscapeRegularExpression(String$, Separator$ = "[][{}()*+?.\\^$|=<>#]")
	Protected NewList StringList.s(), *S.Integer = @String$
	Protected *jc.Character, *c.Character = @String$
	Protected Result.String

	While *c\c
		*jc = @Separator$

		While *jc\c
			If *c\c = *jc\c
				*c\c = 0
				AddElement(StringList())
				If *S <> *c
					StringList() = PeekS(*S)
					AddElement(StringList())
				EndIf
				StringList() = "\" + Chr(*jc\c)
				*S = *c + SizeOf(Character)
				Break
			EndIf
			*jc + SizeOf(Character)
		Wend

		*c + SizeOf(Character)
	Wend
	AddElement(StringList())
	StringList() = PeekS(*S)
	ListToString(@Result, StringList())
	ProcedureReturn Result\s
EndProcedure


Define S.s = "This is a test string \to .see$ if split$$$ and join are working$"
Debug "|" + S + "|"
Debug "|" + EscapeRegularExpression(S) + "|"
Quin
Addict
Addict
Posts: 1135
Joined: Thu Mar 31, 2022 7:03 pm
Location: Colorado, United States
Contact:

Re: EscapeRegularExpression() function?

Post by Quin »

Thanks, AZJIO! Works like a charm!
Post Reply