Jump to content

Regex split quoted strings with escape quotes


Recommended Posts

Can anyone tell me why this isn't working?..

#include <array.au3>
$regexp = StringRegExp("test 'a b c'", "'([^']|'')*'|\S+", 3)
_ArrayDisplay($regexp)

trying to split this "test 'a b c'  'some other '' test'' ...'" into:

0: test

1: 'a b c'

2: ...

but it gives me:

0: test

1: c

Link to comment
Share on other sites

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
Link to comment
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 #

Link to comment
Share on other sites

$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

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

Link to comment
Share on other sites

@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..
Link to comment
Share on other sites

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

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

Link to comment
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

Link to comment
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
 Share

×
×
  • Create New...