Skip to content
Advertisement

How to split with Dot without splitting links [duplicate]

I want to split on dot (.) but I don’t want to splits the links.

Let’s say the string is –

<p>This is a paragraph. I want to split it. <a href="somesite.com">Link</a>

Expected Output –

'<p>This is a paragraph' ,'I want to split it' ,'<a href="somesite.com">Link</a>'

Current Output –

'<p>This is a paragraph' ,'I want to split it' ,'<a href="somesite', 'com">Link</a>'

Note that I don’t want the link to split. Also, I know you can split it using .split(".") but how can I not split that link?

Advertisement

Answer

Solution 1: Strings objects have a method called ‘split’:

s = 'google.com'

splitted = s.split('.')

print(splitted)

>>> ['google', 'com']

That takes a string and split by a substring such as ‘.’.

Solution 2: find the position of ‘.’ in the string, then split it manually:

s = 'google.com'

idx = s.indexOf('.')

first = s[:idx]

sec = s[idx:]

print(first)
>>> google

print(sec)
>>> .com
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement