0

I'm trying to rewrite some text that contains URLs such as:

"/route/id"
"/base/route/id"
"/route2/id"
"/base/route2/id"

of which there will be many in the text. The quotes are part of the text being matched.

All these URLs need to be of the format "/base/..." so I need to rewrite "/ to "/base/ unless the "/base is already there. Which is the bit I'm struggling with. I can replace the "/ but not when it's already followed by base.

2
  • Give us some proper samples and we can help you write something that handles an actual piece of data. Commented Jul 11, 2013 at 10:39
  • Do you want the preserve the format of the file? And can there be more than one URL per line? Commented Jul 11, 2013 at 11:01

2 Answers 2

2

No need for Regex:

var listOfThingsAllWithBase = listOfThings.Select(a => a.StartsWith("\"/base") ? a : "\"/base" + a.Substring(1, a.Length - 2));
Sign up to request clarification or add additional context in comments.

5 Comments

Note that this will not handle the existing quotation mark. e.g., "/route/id" will become "/base"/route/id". But a little bit more string manipulation will handle it just fine.
automatic +1 for any correct non-regex answer to a regex question
Thanks dav_i. Maybe I was wasn't clear but this is just one text string and there could be multiple instances on one line. So it's not a list of things.
@tim_barber_7BB: You should update your question with that requirement and some full sample lines then.
@tim_barber_7BB Please update your question with your exact requirements. How will the multiple instances be shown on one line? Is there any more text? This meets what is stated in your question as it stands.
0

My regex is a little rusty, but if I remember correctly, this should find all entries not starting with /base ^(?!/base)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.