2

There are different urls in user_data, I want to change all usernames and passwords in user_data to make them the same. Since the URL directories are different, I want to find the username and password first, starting from the end, and update it with the new username and password.

username and password I want to change: "superuser/123456"

johndoe/123h456 => superuser/123456
james/98c7654 => superuser/123456
robert/32165g4 => superuser/123456

What do you think is the surest and most accurate way to do this using match or gsub ?

local user_data = "http://127.0.0.1/johndoe/123h456/1, http://127.0.0.1/news/james/98c7654/1.jpg, http://127.0.0.1/data/robert/32165g4/1.dat"
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563

1 Answers1

1

Assuming there is only one URL per string and assuming you can anchor the string on the right side (so the username/password are the last path elements), I'd try something like this: user_data:gsub("/[^/]+/[^/]+(/[^/]+)$","/superuser/123456%1").

$ anchors the replacement at the end of the string, [^/]+ means one or more characters, but not /, and %1 replace it with the first capture (which is a first expression in ()).

Paul Kulchenko
  • 25,884
  • 3
  • 38
  • 56