Jump to content

simple regular expression that doesn't work as expected


Recommended Posts

I'm going mad for a regular expression that works not like expected.

The source with the regular expression is this:

#include <array.au3>
$string = FileRead("with_null.CSV")
;$string = FileRead("without_null.CSV")
Local $array = StringRegExp($string, '(\V*)\v{1,2}', 3)
_ArrayDisplay($array) with_null.CSV

I read that "\v" matches a vertical tab (\u000B) so:
question 1) why and where does it find occurrencies of \v? it seems that \v matches new line characters too (unexpectedly)
question 2) why is the result with "with_null.CSV" trunked (unexpected) while the result with "without_null.CSV" is not trunked (expected)?

with_null.CSV

without_null.CSV

Edited by Imbuter2000
Link to comment
Share on other sites

1) \v "matches any vertical whitespace character" (help file) obviously including newline chars

2) The Null char interrupts a char sequence in a string
So unless you absolutely need to keep the Nulls the best way is to remove them

#include <array.au3>
$string = FileRead("with_null.CSV")
$string = StringRegExpReplace($string, '\x00', "")
Local $array = StringRegExp($string, '\V+', 3)
_ArrayDisplay($array)

 

Link to comment
Share on other sites

1) \v "matches any vertical whitespace character" (help file) obviously including newline chars

2) The Null char interrupts a char sequence in a string
So unless you absolutely need to keep the Nulls the best way is to remove them

#include <array.au3>
$string = FileRead("with_null.CSV")
$string = StringRegExpReplace($string, '\x00', "")
Local $array = StringRegExp($string, '\V+', 3)
_ArrayDisplay($array

Ah mikell you are a lifesaver! thank you so much!!!
I didn't read that new part of the AutoIT help page of StringRegExp and I read on this Microsoft page https://msdn.microsoft.com/en-us/library/4edbef7e(v=vs.110).aspx (about .NET Framework) that "\v  Matches a vertical tab, \u000B." that confused me.

Actually what happened is that I used this function that converts CSV to 2D array 
https://www.autoitscript.com/forum/topic/96357-csv-to-2d-array-with-regexp/?do=findComment&comment=692864
This function contains the   "StringRegExp($sCSV & @CR, '(\V*)\v{1,2}', 3)"  and started to truncate the lines when the CSV started to contain the null in the middle of each line.
Unfortunately I don't have the right to modify the CSV.

Is there a way in the regex to cross the null(s)? 

Edited by Imbuter2000
Link to comment
Share on other sites

The regexp engine doesn't have any issue with the NUL character (0x00), but displaying a string containing NULs doesn't work, even if the string itself isn't affected:

Local $s = 'abc' & Chr(0) & 'def'
ConsoleWrite('Length of $s = ' & StringLen($s) & ' --> ' & $s & @LF)
MsgBox(0, '', $s)
Local $t = StringRegExpReplace($s, '\x00', '_')
ConsoleWrite(@LF & $t & @LF)

 

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

Unfortunately I don't have the right to modify the CSV.

You don't need to modify the csv file, but if you want a result to be correctly displayed you have to read the file to a string and then remove the nulls or replace them by a space (which is usual) or by an underscore or whatever you want before applying the regex   :)

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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...