Hi,
Need help in creating regex for the following situation
1. 4 digit codes are listed in a file [ each code is in a separate line
2. I am reading the contents like $codes = Get-Content $FileContainingCodes. This works OK
3. I have another file that i need to search for lines with pattern something like
" a. First 4 chars will be either ABCD or IJKL or WXYZ ,next 4 chars will be any word [a-z0-9A-Z], followed by a hyphen -, then next 4 chars will be again ABCD
or IJKL or WXYZ , next 4 chars will be any word [a-z0-9A-Z], next 6 chars will be a number, next 4 chars will be the $code, then a hyphen and then a C
For e.g. if my $code is 0800 then my pattern should search
ABCD23RT-WXYZFG4D-1234560800-C - will match the pattern
ABDC4567-XYZW9000-1234560801-C - will not match
I tried the following regex patterns and none would work
$str = "ABCD23RT-WXYZFG4D-1234560800-C"
$code = 3920
$pattern = "[ABCD|IJKL|WXYZ]\w{4}-[ABCD|IJKL|WXYZ]\w{4}-\d{6}$code-C"
$str -match $pattern =====> This returns False
$pattern = "[ABCD|IJKL|WXYZ]\w{4}\W[ABCD|IJKL|WXYZ]\w{4}\W\d{6}$code-C"
$str -match $pattern =====> This returns False
However, the following partial patterns work
$pattern = "[ABCD|IJKL|WXYZ]\w{4}-[ABCD|IJKL|WXYZ]\w{4}"
$str -match $pattern =====> This returns True
$pattern = "-\d{6}$code-C" =====> This returns true
$pattern = "[ABCD|IJKL|WXYZ]\w{4}-[ABCD|IJKL|WXYZ]\w{4}-"
$str -match $pattern =====> This returns False ( see the last hyphen?)
I am not sure why my pattern "[ABCD|IJKL|WXYZ]\w{4}-[ABCD|IJKL|WXYZ]\w{4}-\d{6}$code-C"
would not work. Note that the string that i am searching is not in the begining of the line or end of line that would warrant the use of ^ or $. Its somewhere in the middle of a line of a csv file.
Thanks in advance.
Arun