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!
EscapeRegularExpression() function?
Re: EscapeRegularExpression() function?
Maybe
String lib
String lib
Code: Select all
EscapeString(String$ [, Mode])
UnescapeString(String$ [, Mode])
Egypt my love
Re: EscapeRegularExpression() function?
I don't think that'll do it unfortunately, that only supports PB escapes, XML and JSON from what I can see, not regexes.
Re: EscapeRegularExpression() function?
Sample for using Escape with RegularExpression
It may help
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
Re: EscapeRegularExpression() function?
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) + "|"
Re: EscapeRegularExpression() function?
Thanks, AZJIO! Works like a charm!