Search code examples
regextclregexp-replace

tcl regexp related to mix of character and number


My file contains

roll_number : someNumber ; name(mixOfcharactersAndNumbers) filePathInLinux 

such as

roll_number : 81 ; name(JOHN_23_CARTER) /home/directory/johnc
roll_number : 22 ; name(michael21_Lee) /home/directory/mhlee 

My regexp was

regexp {.*name([a-zA-Z0-9]+.*} $line match name 
regexp {.*name(.*).*} $line match name 

It seems like due to mix of both number and character I am not able regexp correctly, am I missing something here?


Solution

  • The main issues are that you did not escape ( char and did not account for the _ char in your first regex. Also, you do not need to match the whole input, it is not necessary with TCL regex that allows partial matches, i.e. matching shorter substrings inside longer ones.

    Just match name( (with name\() and then capture into Group 1 one or more word chars (with (\w+)):

    set a "roll_number : 81 ; name(JOHN_23_CARTER) /home/directory/johnc"
    regexp {name\((\w+)} $a - name
    

    See this TCL demo printing JOHN_23_CARTER.

    Also, see an online regex demo.