Jump to content

General Regex Questions


Recommended Posts

Yet again, I struggle with the simplest problem. I have the below code:

$string = '1234,asdf,0987,poiu' & @CRLF
$regex = '(\V*?)[,\v$]'
$array = StringRegExp($string,$regex,3)
_ArrayDisplay($array,"Results")

which makes

[0]|1234
[1]|asdf
[2]|0987
[3]|poiu
[4]
 

 

I tried this

$string = '1234,asdf,0987,poiu' & @CRLF
$regex = '(\V*?)[,$]'
$array = StringRegExp($string,$regex,3)
_ArrayDisplay($array,"Results")

but that didn't work out either, leaving out the last element of the csv line:

[0]|1234
[1]|asdf
[2]|0987
 

 

How do i get this to just grab all the values on the line of the CSV, assuming that the CSV line could have more than four values and that any one of those values could be empty (expressed as   1,2,,,5,6,7) where 3 and 4 are missing.

$string string represent a single line of a csv file.

Any help greatly appreciated.

Edited by gruntydatsun
Link to comment
Share on other sites

gruntydatsun,

This will grab all of the csv values... 

#include <array.au3>
local $str = '1234,abcd,,,,aaaa,bbbb,,,eeee'
local $array = stringregexp($str,'([^,]+)',3)
_arraydisplay($array)

Not sure what you mean by this

How do i get this to just grab the four values of the CSV

 

kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Link to comment
Share on other sites

Inside sets meta-characters lose their meaning except some like ( - ^ x and some more)

Therefore [$] will search for a real dollar sign rather than for the end of the line. This is the problem with your second regex.

Example of the problem

If StringRegExp("H$ello", "H[$]") Then MsgBox(64, "Info", "Pattern Satisfied")

Problem with your first regex, Decipher - 

The last instance you get is poiu
Reason
(\V*?) : captures poiu
[,\v$] : captures @CR (a vertical space character)

Thereafter the engine moves on for the next match and capture and we are left with only a @LF char.

The reason why you get a blank capture
(\V*?) : captures nothing since there is no non-vertical space char
Note: you used a star therefore if nothing is found then even the pattern is satisfied, if you use a + in place of it you get it.
[,\v$] : captures @LF (a vertical space character)
Pattern is satisfied and the engine stops since the string ends up now.

So, use the following code

#include <Array.au3>

$string = '1234,asdf,0987,poiu' & @CRLF
$regex = '(\V+?)[,\v$]'  ;a '+' will satisfy only when at least a single char is found
$array = StringRegExp($string,$regex,3)
_ArrayDisplay($array,"Results")

Otherwise the following pattern is further simple and is recommended(by me :P) over what you want

#include <Array.au3>

$string = '1234,asdf,0987,poiu' & @CRLF
$regex = '([^,\v]+)'    ;capture anything other than a "," or vertical space char.
$array = StringRegExp($string,$regex,3)
_ArrayDisplay($array,"Results")

Ask if you have any queries

Regards :)

Phoenix XL

Edited by PhoenixXL

My code:

PredictText: Predict Text of an Edit Control Like Scite. Remote Gmail: Execute your Scripts through Gmail. StringRegExp:Share and learn RegExp.

Run As System: A command line wrapper around PSEXEC.exe to execute your apps scripts as System (LSA). Database: An easier approach for _SQ_LITE beginners.

MathsEx: A UDF for Fractions and LCM, GCF/HCF. FloatingText: An UDF for make your text floating. Clipboard Extendor: A clipboard monitoring tool. 

Custom ScrollBar: Scroll Bar made with GDI+, user can use bitmaps instead. RestrictEdit_SRE: Restrict text in an Edit Control through a Regular Expression.

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...