Page 1 of 1

RegularExpression ... IT WORKS BUT WHY ??? ;-)

Posted: Sat Aug 01, 2020 10:21 am
by Joris
Thanks to Michael Vogel :
;viewtopic.php?f=12&t=70463&hilit=NextRe ... ssionMatch

regex=CreateRegularExpression(#PB_Any, ~"[\"#a-zA-Z_ ]+")

This character ~ (called tilde I think) makes it work, but it isn't explained in the PB-helpfile in this use.
Neigther something to find in the REGEX pdf (Regular Expressions Cheat Sheet).

Can someone explain how and when this can or must be used, thanks ?

P.s. if you remove it PureBasic will directly show a red character in the string to let you know something is wrong...

Re: RegularExpression ... IT WORKS BUT WHY ??? ;-)

Posted: Sat Aug 01, 2020 10:37 am
by Marc56us
Joris wrote:This character ~ (called tilde I think) makes it work, but it isn't explained in the PB-helpfile in this use.
Neigther something to find in the REGEX pdf (Regular Expressions Cheat Sheet).
Can someone explain how and when this can or must be used, thanks ?
This is escape sequences. See Literal strings
https://www.purebasic.com/documentation ... rules.html

Code: Select all

Literal strings

    Literal strings are declared using the " character. 
    Escape sequences are supported using the ~ character before the literal string. 
    The allowed escape sequences are:

      \a: alarm           Chr(7)
      \b: backspace       Chr(8)
      \f: formfeed        Chr(12)
      \n: newline         Chr(10)
      \r: carriage return Chr(13)
      \t: horizontal tab  Chr(9)
      \v: vertical tab    Chr(11)
      \": double quote    Chr(34)
      \\: backslash       Chr(92)
Yes, we can use it for a regex.

:wink:

Re: RegularExpression ... IT WORKS BUT WHY ??? ;-)

Posted: Sat Aug 01, 2020 10:52 am
by Joris
But why is this one then wrong ?
regex=CreateRegularExpression(#PB_Any, "[\"#a-zA-Z_ ]+")

Re: RegularExpression ... IT WORKS BUT WHY ??? ;-)

Posted: Sat Aug 01, 2020 11:19 am
by NicTheQuick
Joris wrote:But why is this one then wrong ?
regex=CreateRegularExpression(#PB_Any, "[\"#a-zA-Z_ ]+")
Because you can not use double quotes inside a string without escaping them. And therefore you need to use the syntax for escaped strings: ~""

Re: RegularExpression ... IT WORKS BUT WHY ??? ;-)

Posted: Sat Aug 01, 2020 2:57 pm
by infratec
If you use ~ you can escape with a backslash, if not you have to use #DQUOTE$

Code: Select all

Debug  ~"[\"#a-zA-Z_ ]+"

Debug "[" + #DQUOTE$ + "#a-zA-Z_ ]+"