-3

I want to use a regex to do a search/replace in an open office spreadsheet.

I need to insert an authentication component in a list of urls right after the last forward slash.

I tried [/]$ and (\/)$ with no luck.

Example input: https://testdomain.com/dir1/file.pdf

Desired output: https://testdomain.com/dir1/authcode123file.pdf

jahroy
  • 22,322
  • 9
  • 59
  • 108
  • The last `/` in a string: `/(?=[^/]*$)`. – The Guy with The Hat Jun 06 '14 at 16:20
  • 3
    **Try writing something yourself** and then if it doesn't work, show us specifically what you did so we can help you along. **You start it, and then we help. We don't write it for you.** Show us the actual code that you've tried, and then describe what happened and what's not right, and then we can help you from there. Chances are you'll get pretty close to the answer if you just try it yourself first. – Andy Lester Jun 06 '14 at 16:25
  • ... Or spend one second searching the interwebz: [one](http://stackoverflow.com/a/1150638/778118), [two](http://stackoverflow.com/q/8590052/778118), [three](http://stackoverflow.com/q/19776979/778118). – jahroy Jun 06 '14 at 16:30
  • I couldn't get this to work. I even went out to regex101.com and it didn't work. Really appreciate the help though. – user2657339 Jun 06 '14 at 16:32
  • Fair enough, I understand. I tried \/$ to search from the end of the string back and that didn't work. I have searched the web and stackoverflow with no luck – user2657339 Jun 06 '14 at 16:33
  • What programming language/enviromnent are you working in? JavaScript, Java, Bash, C#, Perl, sed, Python?? It makes a difference. And... my first comment contains 3 links to related questions on this site. – jahroy Jun 06 '14 at 16:37
  • Very true and willing to take any noob initiation consequences necessary to get this figured out. I am working in the open office Calc spreadsheet application. The search and find function has a regex feature that I am using to search the table. – user2657339 Jun 06 '14 at 16:52
  • Cool... I just opened up OpenOffice and gave it a try. See [my answer](http://stackoverflow.com/a/24087226/778118). – jahroy Jun 06 '14 at 17:07

2 Answers2

0

Im not sure about your procedure but how about something like this:

enter image description here

martin
  • 289
  • 1
  • 6
  • This is very close to what I need and in fact finds the last slash and the dir1 as well. Unfortunately I just need the last slash to be identified, selected, then I will input what is needed from there. It would get the job done but not in the open office application. This is turning out to be tougher than I thought it would be. – user2657339 Jun 06 '14 at 16:59
0

Try this:

Search for: (.*/)(.*)$

Replace with:: $1authcode123$2

Explanation:

 (.*)       -  Anything inside two parens "()" is called a capture
               group.  The text that matches the pattern inside the
               first set of parens is stored in "capture group 1".
               You can use $1 to refer to this capture group when
               you do your replace.  Likewise $2 will refer to the
               second capture group.

  .         -  A period represents ANY CHARACTER.

  .*        -  The asterisk "*" means "zero of more of the precedding
               pattern."  So .* would mean "zero or more occurrences
               of ANY CHARACTER." 

 (.*/)    -  zero or more characters followed by a slash 
              (the results of this capture are stored in $1)

 (.*)$    -  zero or more chars followed by the end of the string 
              (the results of this capture are stored in $2)

Screenshot

However, you haven't shared all of the details of your requirements, so I don't know if this answer is complete.

For example, what would you do for a url that has no slashes?

jahroy
  • 22,322
  • 9
  • 59
  • 108
  • I'm not sure how this works but this is EXACTLY what I needed. Works like a charm. All the records will include the exact format mentioned with slashes, so this is GREAT. Can't thank you enough. I have taken more of your time that I am comfortable with, however, if you could explain your solution that would be a life saver. God Bless you! Will help others if I can and spread the word of your help. – user2657339 Jun 06 '14 at 17:23
  • I tried to offer an explanation of the regex above the screenshot. I'll try to improve it. – jahroy Jun 06 '14 at 17:24
  • I will input this regex in regex101.com to see how this works as well. – user2657339 Jun 06 '14 at 17:25
  • @user2657339 - I updated my explanation to include a little bit about capture groups and some of the special characters. I also edited your question to add the `openoffice-calc` tag, which is a great way to attract experts to see your question. – jahroy Jun 06 '14 at 17:39
  • Very, very, nice and helpful explanation. Very much appreciated. Thank you! – user2657339 Jun 06 '14 at 17:51
  • No prob - glad I could help :-) – jahroy Jun 06 '14 at 17:54