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

Just starting out? Need help? Post your questions and find answers here.
Joris
Addict
Addict
Posts: 885
Joined: Fri Oct 16, 2009 10:12 am
Location: BE

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

Post 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...
Last edited by Joris on Sat Aug 01, 2020 10:47 am, edited 1 time in total.
Yeah I know, but keep in mind ... Leonardo da Vinci was also an autodidact.
Marc56us
Addict
Addict
Posts: 1477
Joined: Sat Feb 08, 2014 3:26 pm

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

Post 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:
Joris
Addict
Addict
Posts: 885
Joined: Fri Oct 16, 2009 10:12 am
Location: BE

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

Post by Joris »

But why is this one then wrong ?
regex=CreateRegularExpression(#PB_Any, "[\"#a-zA-Z_ ]+")
Yeah I know, but keep in mind ... Leonardo da Vinci was also an autodidact.
User avatar
NicTheQuick
Addict
Addict
Posts: 1224
Joined: Sun Jun 22, 2003 7:43 pm
Location: Germany, Saarbrücken
Contact:

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

Post 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: ~""
The english grammar is freeware, you can use it freely - But it's not Open Source, i.e. you can not change it or publish it in altered way.
infratec
Always Here
Always Here
Posts: 6817
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

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

Post 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_ ]+"
Post Reply