How can I remove brackets next to the link in Python?
Question:
I have a string
Some sentance startx here blah blah [Example](https://someSite.com/another/blah/blah)
and I want this string to become this one:
Some sentance startx here blah blah Example
I have tried this regex:
"[[]](S*(https|http)*.(ru|com)S*"
but I get this:
Some sentance startx here blah blah [Example
The code:
pattern = r"[[]](S*(https|http)*.(ru)S*"
text = re.sub(pattern, '', text)
Answers:
Use
[([^][]*)](http[^s()]*)
Replace with 1
.
See regex proof.
Python code snippet:
text = re.sub(r'[([^][]*)](http[^s()]*)', r'1', text)
maybe like this:
string = '[Example](https://someSite.com/another/blah/blah)'
string = string.split("[")[1].split("]")[0]
print(string)
I’m not sure why you want to build a pattern for the whole string and then replace everything with an empty string. you could just search for everything in the []
brackets.
string = "[Example](https://someSite.com/another/blah/blah)"
pat = r"^[([^][]+)]"
result = re.search(pat, string).group(1)
print(result)
Example
Check the pattern at Regex101.
I have a string
Some sentance startx here blah blah [Example](https://someSite.com/another/blah/blah)
and I want this string to become this one:
Some sentance startx here blah blah Example
I have tried this regex:
"[[]](S*(https|http)*.(ru|com)S*"
but I get this:
Some sentance startx here blah blah [Example
The code:
pattern = r"[[]](S*(https|http)*.(ru)S*"
text = re.sub(pattern, '', text)
Use
[([^][]*)](http[^s()]*)
Replace with 1
.
See regex proof.
Python code snippet:
text = re.sub(r'[([^][]*)](http[^s()]*)', r'1', text)
maybe like this:
string = '[Example](https://someSite.com/another/blah/blah)'
string = string.split("[")[1].split("]")[0]
print(string)
I’m not sure why you want to build a pattern for the whole string and then replace everything with an empty string. you could just search for everything in the []
brackets.
string = "[Example](https://someSite.com/another/blah/blah)"
pat = r"^[([^][]+)]"
result = re.search(pat, string).group(1)
print(result)
Example
Check the pattern at Regex101.