View Full Version : Reg expres using match() with nawk and [

09-02-2003, 04:45 PM
Hiya, should be simple, but can't seem to work out how to match a square bracket

Take this as a string, for example:
sundries_matrix[2] = "Sundry 3";

current equals the above string

if(match(current, "sundries_list.2. = \"Sundry 3\";") != 0) returns 1 which is fine

But I need to match based on the square brackets so

if(match(current, "sundries_list\[2\] = \"Sundry 3\";") != 0) returns 0

I just cannot seem to get nawk to recognise square bracket as a character instead of a special something or other, is there any chance I can do it? what does nawk see [ or ] as?

The reason I need the brackets in, is so I can use substr to cut out the characters (numbers) between the open and close brackets to allow for infinite growth

Thx for any ideas, or god forbid, the answer :D

09-02-2003, 05:17 PM
worked it out by trial and error (based on previous language experience)

if (match(current, "sundries_list.2. = \"Sundry 3\";") != 0)
left_br_idx = match(current, "\\[")
right_br_idx = match(current, "\\]")

# printf("\nleft_br_idx = %d | right_br_idx = %d", left_br_idx, right_br_idx)

# starting from search_index to search_index+1
result = substr(current, (left_br_idx + 1), (right_br_idx-1 - left_br_idx))s