regex - Non-greedy match from end of string with regsub -


i have folder path following:

/h/apps/new/app/k1999 

i want remove /app/k1999 part following regular expression:

set folder "/h/apps/new/app/k1999" regsub {\/app.+$} $folder "" new_folder 

but result /h: many elements being removed.

i noticed should use non-greedy matching, change code to:

regsub {\/app.+?$} $folder "" new_folder 

but result still /h. what's wrong above code?

non-greedy means try match least amount of characters , increase amount if whole regex didn't match. opposite - greedy - means try match characters can , reduce amount if whole regex didn't match.

$ in regex means end of string. therefore something.+$ , something.+?$ equivalent, 1 more retries before matches.

in case /app.+ matched /apps , first occurrence of /app in string. can fix being more explicit , adding / follows /app:

regsub {/app/.+$} $folder "" new_folder 

Comments

Popular posts from this blog

Android : Making Listview full screen -

javascript - Parse JSON from the body of the POST -

javascript - Chrome Extension: Interacting with iframe embedded within popup -