1

In Python, I would like to replace the "be" into "nl" in the following string:

http://test.nl/be/nl/product/abc-dadf-adsfsw-12234/

BUT!! I want it to check the part BEFORE /product/, because if the string after /product/ contains a "be" it has to remain the same.

Examples:

http://test.nl/nl/nl/product/abc-dadf-be-adsfsw-12234/ (part BEFORE /product/ contains no be, so has to remain the same)

http://test.nl/be/nl/product/abc-dadf-be-adsfsw-12234/ (part BEFORE /product/ contains be, so it has to be replaced with nl, so it becomes http://test.nl/nl/nl/product/abc-dadf-be-adsfsw-12234/)

http://test.be/nl/nl/product/abc-dadf-be-adsfsw-12234/ (part BEFORE /product/ contains be, so it has to be replaced with nl, so it becomes http://test.nl/nl/nl/product/abc-dadf-be-adsfsw-12234/)

3
  • Could you give an example of when "be" has to remain the same? Commented Apr 4, 2014 at 11:43
  • To clarify: What would your url be after replacement? Commented Apr 4, 2014 at 11:44
  • What did you try that didn't work? Are the strings you're doing the replacement in always in this exact format of "http://test.nl/be/nl/product/.*"? Commented Apr 4, 2014 at 11:45

3 Answers 3

1

Use positive lookahead for this.

url = '''http://test.nl/be/nl/product/abc-dadf-adsfsw-12234/'''
url = re.sub("/be/(?=.*/product/)", "/nl/", url)
# or using word boundary around `be` to handle .be/ .be. etc
# url = re.sub("\bbe\b(?=.*/product/)", "nl", url)
print url

This positive lookahead (?=.*/product/) is checking that there is /product/ exists after the /be/ in front.

But remember, this regex is true for any /be/ which are before /product/. In general I am talking about multiple occurrence.

Sign up to request clarification or add additional context in comments.

2 Comments

Your answer works and with least code, thanks!:) Is there a possibility to double check or it contains /product/ OR /forum/ ?
@ErikVandeVen everything. Try this (?=.*/(?:product|forum|another|more|any)/)
0

The simplest way I can think of doing that is splitting the string with a regular expression. Here is some code that should give you the desired output.

import re #python's regular expression module
string = 'http://test.nl/be/nl/product/abc-dadf-adsfsw-12234/'

search = re.match('(.*)(product.*)', string)
first_part = search.group(1)
next_part = search.group(2)

first_part = first_part.replace('be', 'nl')

new_string = first_part + next_part
print(new_string)

Comments

0

Option without regexp (and no imports):

elements = raw_input('URL?\n').split('/')

for i in range(0, elements.index('product')):
    elements[i] = elements[i].replace('be', 'nl')

print '/'.join(elements)

Tests:

http://test.nl/nl/nl/product/abc-dadf-be-adsfsw-12234/
  -> http://test.nl/nl/nl/product/abc-dadf-be-adsfsw-12234/

http://test.nl/be/nl/product/abc-dadf-be-adsfsw-12234/
  -> http://test.nl/nl/nl/product/abc-dadf-be-adsfsw-12234/

http://test.be/nl/nl/product/abc-dadf-be-adsfsw-12234/
  -> http://test.nl/nl/nl/product/abc-dadf-be-adsfsw-12234/

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.