TheAutomator

Regex split quoted strings with escape quotes

10 posts in this topic

This matches one "not single quote" character.

[^']

 

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

12 minutes ago, Malkey said:

This matches one "not single quote" character.

[^']

 

yes but it's in a (group)* so it matches everything between single quotes that is not a quote itself unless you type 2 after each other, so you can escape a single quote by typing 2 like in vbscript: msgbox "a ""test"" here"
i hope you know what i mean :)

'([^']|'')*'
Edited by TheAutomator

Share this post


Link to post
Share on other sites

Try this.

#include <array.au3>
$regexp = StringRegExp("test 'a b c' string 'd e f'", "'([^']+)'|[^']+", 3)
_ArrayDisplay($regexp)


$string = "a ""test"" here" ;  Is correct for single double quotes arround "Test"
ConsoleWrite($string & @CRLF)

$string1 = 'a ""test"" here' ;  Will give  two double quotes arround ""Test""
ConsoleWrite($string1 & @CRLF)

 

Share this post


Link to post
Share on other sites

Uhm okay how do I explain it better..
lets replace the quotes by #

#([^#]|##)*#|\S+

this works perfectly with the vbscript.regexp object as the pattern so why not in autoit?

the string # test ## 123 ## done# abc123 #some other test# should split into:

# test ## 123 ## done#

abc123

# some other test #

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

$str = "# test ## 123 ## done# abc123 #some other test#"
msgbox(0, '' , stringmid($str , 1 , stringinstr($str , "#" , 0 , 6))  & @CR &  stringmid($str , stringinstr($str , "#" , 0 , 6) + 1 , stringinstr($str , "#" , 0 , 7) - stringinstr($str , "#" , 0 , 6) - 1) & @CR & stringmid($str , stringinstr($str , "#" , 0 , 7)))

also this regex way :)

#include <array.au3>

$str = "# test ## 123 ## done# abc123 #some other test#"
$split = stringreverse(_ArrayToString(StringRegExp(stringreverse($str) , "(#.*?#)(.*?)(#.*#)" , 3) , @CR))
msgbox(0, '' , $split)

 

Edited by iamtheky

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

@iamtheky

Using this:-

"#some other test# abc123 # test ## 123 ## done#"

as the test string,  both your examples do not return an array like this:-

#some other test#
 abc123
# test ## 123 ## done#

as I expected  But, we could differ in what to expect.

Malkey

Edited by Malkey
Added "I" in "as I expected." and etc..

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

Neither of my examples return an array at all, but are the splits not in the expected locations?  Are you not entertained!?

ah, nvm, i now see the edge case you manufactured.  As well, there are plenty of arrangements that blow up all of the proposed solutions, thats on the OP tho.

Edited by iamtheky

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Share this post


Link to post
Share on other sites
18 hours ago, Malkey said:

I suppose vbscript.regexp is a different flavour.  AutoIt uses Perl-compatible Regular Expressions (PCRE).

#include <array.au3>
$regexp = StringRegExp("# test ## 123 ## done# abc123 #some other test#", "(#(?:[^#]+|##)*#)|[^#]+", 3)
_ArrayDisplay($regexp)

 

Ah i see, so that's why it doesn't work, just a different type of regex language..

I wanted to split a string into tokens like most parsers do with quoted strings.


Thanks for the explanation and help :)

Regards

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now

  • Similar Content

    • rcmaehl
      By rcmaehl
      Hi all,
      I still suck at regex as always and I need some help. According to the regex tester I normally use this should be working fine but it doesn't....
      StringRegExp($sString, "\A[1-9]+[0-9]*(\-[1-9]+[0-9]*)?,*\Z") I basically want to match:
      all numbers EXCEPT 0, but including 10, 20, etc with each number separated by a comma and allowing a "-" separated range as a value For example:
      1-5,7,10-12 I've spent a couple hours modifying it but I'm not sure where I've gone wrong. Any help would be appreciated!
    • goodmanjl531
      By goodmanjl531
      Hope someone can help as quotes within quotes is always confusing.  I am creating a silent install of printer software and want to call via runwait comspec line.
      The call within a cmd line is below
      samsung.exe /s /n"Samsung SL-M3220ND" /p"xxx"
      xxx will be stored as a variable  $PrinterIP which would be as example  10.10.10.20    I need to use a variable as i am doign this for a business and need it to be based on the store i install at that has a specific IP range.
       
      I started with the below as 2 possibilities but no luck..
      RunWait(@ComSpec & "/c " & "samsung.exe /s /n""Samsung SL-M3220ND"" /p""" & $PrinterIP ") RunWait(@ComSpec & "/c " & "samsung.exe /s /n'Samsung SL-M3220ND' /p'" & $PrinterIP '") but i cant get it to compile as i have errors on the line..  thanks in advance.
    • ISI360
      By ISI360
      Hi!

      I need a little bit help from some RegEx experts please:
      I would make my ISN AutoIt Studio faster when generating the scripttree. And what would be better to do this via regex?
      Problem is i am not really good at this regex stuff. So maybe someone could help me here.
       
      The challange is to get all Global Variables from a script via RegEx in a Array.
      Here is a example script with some tests:
      Global $Var1 = 1234 Local $Local_Var = 1234 $Ignore_me_too = 1234 Global $Var2 = 1234, $var3 = 1242 Global $ahIcons[30], $ahLabels[30] Global Const $Var4 = iniread($inivar1,"jj","jj","") , $var5= iniread($inivar2,"jj","jj","") Global $Var_String = "was" Global $Array_Test[16] = [1,15,16,0,31,15,25,15,25,30,8,30,8,15,1,15] Global Enum $MARGIN_SCRIPT_NUMBER = 0, $MARGIN_SCRIPT_ICON, $MARGIN_SCRIPT_FOLD Global Const $Delim = '\', $Delim1 = '|' Global $hard1 = "a", _ $hard2 = "b", _ $hard3 = "c"  
      The returning array should look like this:
      $Var1 $Var2 $var3 $Var4 $var5 $Var_String $Array_Test $MARGIN_SCRIPT_NUMBER $MARGIN_SCRIPT_ICON $MARGIN_SCRIPT_FOLD $Delim $Delim1 $hard1 $hard2 $hard3  
      I already made some success with a expression i found in the SciTE Jump Tool:  (\$\w+)(?:[\h\[.=+*/^,)\-])?
      This nearly returns the perfect results. But it does not check if it´s a global variable (with the const and enum options) and also returns variables in commands (for example $inivar1)
      I also found this regex: (?im:^(?=Global|Const|Enum|Static)(?:Global)?\h*(?:Const|Enum|Static)?(?:(?<=Enum)\h+Step\h+[+*-]\d+)?\h*)([^\r\n .\=]+)
      This returns also usefull results...but trying to understand this explodes my head

      Maybe someone can help me here?
      Thanks in advance!
    • anthonyjr2
      By anthonyjr2
      Hi guys,
      I am pretty bad with regex, and am having some trouble trying to come up with an expression for a certain type of string. Basically I want to be able to tell if a string is of the format:
      AA#####A
      Where the A's are any letter from A-Z and the #'s are any digit from 0-9.
      I've been playing around with a regex tester online for a while but I can't really seem to grasp the concept very well. Could anyone give me any tips?
      This isn't exactly an AutoIt specific question which is why I didn't post it in General Help & Support.
    • tezhihi
      By tezhihi
      I have a file (see attached file) with a string all line and this problem on here is I want to separate all $00:, $03:, $10:, $20:, $25:, $30:, $40:, $45:, $110:, $115:, $120: and $T. It's mean that each $ with value start a new line ( a new paragraph). I tried with Regular Expression in notepad++ ex:
      Find ($00:, $01:, $03: and so on) with regex (\$)([0-9]+): and replace is \r\n\1\2 (I think \r\n is @CRLF (not sure :() ) Find $T with regex (\$T)(.*?)(\$T) and replace is \1\2\r\n\3 When I try these regex to replace in notepad on StringRegexReplace the results is incorrect . I have read some example simple about regex. Please advise me how to do that with some example on autoit . The result will be in attached photo. Thanks 
      ahihi.txt