• Want to receive the latest contracting news and advice straight to your inbox? Sign up to the ContractorUK newsletter here. Every sign up will also be entered into a draw to WIN £100 Amazon vouchers!

Regular Expressions

  • Thread starter Thread starter Geffa
  • Start date Start date
G

Geffa

Guest
Just starting (hopefully) to get my head around regular expressions when I have come across a real life problem where I am not sure if they can be utilised.

I am receiving in a message a fixed length fixed format string. I wondered if I could use a regular expression to easily extract the information. I know I can do it by straightforward string manipulation but I thought I may have an opportunity to use regular expressions.

e.g. "AAAABBBCCCCC"
I would like a regular expression that produced a match collection containing
AAAA
BBB
CCCCC

where A,B and C are any alphanumeric character. I need to extract the first 4 characters as the first match, the next 3 as the second, the next 5 asthe third and so on.

I am using the RegExp class provided by VBScript, in case that has any relevance to a possible solution.

TIA
 
This code fragment works in Perl, don't know that much about javascript.


$string = "0123456789ab";

$string =~ /^([A-Z0-9]{4})([A-Z0-9]{3})([A-Z0-9]{5})/i;

print "$1, $2, $3\n";
 
> I am receiving in a message a fixed length fixed format
> string.

substring works fine for this sort of tasks, less overhead, especially in VBscript.

note: I love perl and reg exps but sometimes substring is just fine.
 
Thanks for the info. Sorry for the delay in getting back - day off today for 3yr old daughter's birthday.

PerlOfWisdom - The pattern you showed was similar to the one I thought and tried but to no avail. I am beginning to think that the VBScript class does not support the () part of regular expressions (although the documentation states otherwise). I have pasted examples from the net and books and it fails to work on every occasion. I tried this using .Net regular expressions and it worked a dream.

AtW - I appreciate that using a regular expression may not have been the ideal way of doing this. However, sometimes the way to get ones head around a new technique is to put it to effect in a practical circumstance rather than a contrived one. I wasn't sure if I would have implemented the final solution using a regular expression but was using it as a learning exercise.
 
RegExps are part of JavaScript since v1.3 (or v1.4 more like it --never had to use them in JS). Previously you could use Java for it, but it was really messy and crappy.
 
Not sure where Javascript has crept into this. I am using VB6 and referencing the "Microsoft VBScript Regular Expressions" class.

The documentation on this does list the match and capture pattern () but does not appear to work.
 

Advertisers

Back
Top