Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    Regular Coder
    Join Date
    Jul 2002
    Posts
    165
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Reg expres using match() with nawk and [

    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

  • #2
    Regular Coder
    Join Date
    Jul 2002
    Posts
    165
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Cool worked it out

    worked it out by trial and error (based on previous language experience)

    Code:
    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


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •