PureBasic Forum
https://www.purebasic.fr/english/

RegularExpression ... IT WORKS BUT WHY ??? ;-)
https://www.purebasic.fr/english/viewtopic.php?f=13&t=75792
Page 1 of 1

Author:  Joris [ Sat Aug 01, 2020 10:21 am ]
Post subject:  RegularExpression ... IT WORKS BUT WHY ??? ;-)

Thanks to Michael Vogel :
;https://www.purebasic.fr/english/viewtopic.php?f=12&t=70463&hilit=NextRegularExpressionMatch

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...

Author:  Marc56us [ Sat Aug 01, 2020 10:37 am ]
Post subject:  Re: RegularExpression ... IT WORKS BUT WHY ??? ;-)

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:
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:

Author:  Joris [ Sat Aug 01, 2020 10:52 am ]
Post subject:  Re: RegularExpression ... IT WORKS BUT WHY ??? ;-)

But why is this one then wrong ?
regex=CreateRegularExpression(#PB_Any, "[\"#a-zA-Z_ ]+")

Author:  NicTheQuick [ Sat Aug 01, 2020 11:19 am ]
Post subject:  Re: RegularExpression ... IT WORKS BUT WHY ??? ;-)

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: ~""

Author:  infratec [ Sat Aug 01, 2020 2:57 pm ]
Post subject:  Re: RegularExpression ... IT WORKS BUT WHY ??? ;-)

If you use ~ you can escape with a backslash, if not you have to use #DQUOTE$

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

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

Page 1 of 1 All times are UTC + 1 hour
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/