Jump to content

StringRegExpReplace - Help with CSV file and replacing a comma :)


Go to solution Solved by Malkey,

Recommended Posts

On 1/23/2019 at 1:49 PM, jchd said:

The regexp posted which you seem to use is setup to replace commas in strings enclosed in double quotes within the whole subject.

Local $FF = 'abc,"SAPPS,DAVID T., DDS, PC",456,"1,2.,3",1,2.,3', $string

Do
    $FF = StringRegExpReplace($FF, '((?:^|,)"[^,"]*),(?=[^"]*")', "$1")
Until @extended = 0
MsgBox(0, "", $FF)

The string you posted is just a plain AutoIt string.

Hi jchd,

I am still having trouble with this. Please try this string.

$FF = 'Shipping Department,"Hu-Frie Mfg. Co., LLC",7 E TOUCHY AVE,,Des Plaines,IL,60018,US,.,"SAPPS,DAVID T., DDS, PC",SAPPS ORTHODONTICS,189 STEWART LANE,JONESBORO,TN'

Thank you

Link to comment
Share on other sites

45 minutes ago, mikell said:

It works for me (meaning : remove commas in the parts which are inside double quotes)
Could you explain your issue ?

Here is the entire string, vs splitting out what I think the problem area is. Please try this:

$FF = '7.1,00000776KN,320639,US,1/5/2019,XXXXXXXXXX,E,,,USD,26694.26,12/20/2018,,XXXXXXXXXXXXX,,4410628,82576404,F/C,1,0,XXXXXXXXXXXXXXXXXX,XXXXXXXX,XXXXXXXXX,,,,0.9,L,1,L,PKG,,,4,SHP,FC,,,,,,,,FRT,3,XXXXXXXX XXXXXX XXXXX,1,,0,,USD,1.29,7.3,,0,0,,0,0,0,0,0,1/16/2019,,,,XXXXXX XXXXXXXXX,"Hu-Frie Mfg. Co., LLC",7 E TOUCHY AVE,,Des Plaines,IL,60018,US,.,"SAPPS,DAVID T., DDS, PC",SAPPS ORTHODONTICS,189 STEWART LANE,JONESBORO,TN,28199,US,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,0,,,,0,,,,,0,,0,0,,,0,,,,,,,,,,,,0,,,,,,,0,0,0,0,0,0,,,0,0,0,,,,,,2,,,,,,0,0,0,0,0,0,0,,,,,0,,,0,,0,,,,,,,,,,,,,,,,,,,,,0,,,,,,,,,,,,,,,,,,,,,,,,,,,XXXXXXXXXXXXXXX'

Link to comment
Share on other sites

11 minutes ago, mikell said:

Still working for me with this string. What should be the result YOU expect ?

Commas to be stripped, except for those used to delimit the string

Here is what I am using:

$OLB_File_Line = FileOpen($Input_File, $FO_READ)
$FF = FileReadLine($OLB_File_Line , $i)
$FF = StringRegExpReplace($FF, '((?:^|,)"[^,"]*),(?=[^"]*")', "$1")

Edited by NSearch
Link to comment
Share on other sites

Works for me as well. There are only four commas to remove:

"Hu-Frie Mfg. Co., LLC" becoming "Hu-Frie Mfg. Co. LLC"
and
"SAPPS,DAVID T., DDS, PC" becoming "SAPPSDAVID T. DDS PC"

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

1 minute ago, jchd said:

Works for me as well. There are only four commas to remove:

"Hu-Frie Mfg. Co., LLC" becoming "Hu-Frie Mfg. Co. LLC"
and
"SAPPS,DAVID T., DDS, PC" becoming "SAPPSDAVID T. DDS PC"

test_csv.txt

Would you please test the attached file?

$OLB_File_Line = FileOpen($Input_File, $FO_READ)
$FF = FileReadLine($OLB_File_Line , $i)

$X = inputbox("Before","",$FF)
$FF = StringRegExpReplace($FF, '((?:^|,)"[^,"]*),(?=[^"]*")', "$1")

$X = inputbox("After","",$FF)

Link to comment
Share on other sites

You use only ONE StringRegExpReplace while the posted code uses a loop. Hence you're replacing only the first instance.

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

5 minutes ago, jchd said:

You use only ONE StringRegExpReplace while the posted code uses a loop. Hence you're replacing only the first instance.

I believe "$1" is the parameter that you're referring to. What should I change it to, so that it removes all commas contained within quotes, for all instances?

$FF = StringRegExpReplace($FF, '((?:^|,)"[^,"]*),(?=[^"]*")', "$1")

 

 

Link to comment
Share on other sites

Use the code I reposted!

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

Here a simple way that will work ALL the time, but you wont like it tho. :

#include <Constants.au3>
#include <Array.au3>

Opt ("MustDeclareVars", 1)

Global Const $FF = '7.1,00000776KN,320639,US,1/5/2019,XXXXXXXXXX,E,,,USD,26694.26,12/20/2018,,XXXXXXXXXXXXX,,4410628,82576404,F/C,1,0,XXXXXXXXXXXXXXXXXX,XXXXXXXX,XXXXXXXXX,,,,0.9,L,1,L,PKG,,,4,SHP,FC,,,,,,,,FRT,3,XXXXXXXX XXXXXX XXXXX,1,,0,,USD,1.29,7.3,,0,0,,0,0,0,0,0,1/16/2019,,,,XXXXXX XXXXXXXXX,"Hu-Frie Mfg. Co., LLC",7 E TOUCHY AVE,,Des Plaines,IL,60018,US,.,"SAPPS,DAVID T., DDS, PC",SAPPS ORTHODONTICS,189 STEWART LANE,JONESBORO,TN,28199,US,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,0,,,,0,,,,,0,,0,0,,,0,,,,,,,,,,,,0,,,,,,,0,0,0,0,0,0,,,0,0,0,,,,,,2,,,,,,0,0,0,0,0,0,0,,,,,0,,,0,,0,,,,,,,,,,,,,,,,,,,,,0,,,,,,,,,,,,,,,,,,,,,,,,,,,XXXXXXXXXXXXXXX'
Global Const $rep = " | "
Global $arr = StringSplit ($FF,""), $Start = False, $Final = ""

For $i = 1 to $arr[0]
  if $arr[$i] = '"' Then $Start = not $Start
  if $arr[$i] = ',' and $Start then $arr[$i] = $rep
  $Final &= $arr[$i]
Next

Local $test = _ArrayToString ($arr, "", 1)

MsgBox ($MB_SYSTEMMODAL,"",$test = $Final)

Edit : :lol:

Edited by Nine
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...