Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3875640
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T22:17:25+00:00 2026-05-19T22:17:25+00:00

Given a string, I want to replace all links within it with the link’s

  • 0

Given a string, I want to replace all links within it with the link’s description. For example, given

this is a [[http://link][description]]

I would like to return

this is a description

I used re-builder to construct this regexp for a link:

\\[\\[[^\\[]+\\]\\[[^\\[]+\\]\\]

This is my function:

(defun flatten-string-with-links (string)
    (replace-regexp-in-string "\\[\\[[^\\[]+\\]\\[[^\\[]+\\]\\]"
                (lambda(s) (nth 2 (split-string s "[\]\[]+"))) string))

Instead of replacing the entire regexp sequence, it only replaces the trailing “]]”. This is what it produces:

this is a [[http://link][descriptiondescription

I don’t understand what’s going wrong. Any help would be much appreciated.

UPDATE: I’ve improved the regex for the link. It’s irrelevant to the question but if someone’s gonna copy it they may as well get the better version.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T22:17:26+00:00Added an answer on May 19, 2026 at 10:17 pm

    Your problem is that split-string is clobbering the match data, which
    replace-regexp-in-string is relying on being unchanged, since it is going to
    go use that match data to decide which sections of the string to cut out. This
    is arguably a doc bug in that replace-regexp-in-string does not mention that
    your replacement function must preserve the match data.

    You can work around by using save-match-data, which is a macro provided for
    exactly this purpose:

    (defun flatten-string-with-links (string)
        (replace-regexp-in-string "\\[\\[[a-zA-Z:%@/\.]+\\]\\[[a-zA-Z:%@/\.]+\\]\\]"
                    (lambda (s) (save-match-data
                             (nth 2 (split-string s "[\]\[]+")))) string))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given a string like this: <a href=http://blah.com/foo/blah>This is the foo link</a> ... and a
Given a datetime string like this: 2012-06-19 03:44:39 I want to use it to
Given this string random_string= '4' i want to determine if its an integer, a
Given a string such as: a:2:{i:0;s:1:1;i:1;s:1:2;} I want to find every integer within quotes
I want to use this for an object factory: Given a string, create a
I want to replace all occurences of a pattern in a string by another
I want to unescape all nested quotes within a string. The following examples are
I want to replace a particular string in (the text of) all the descendant
There is a string(just for testing) and I want to replace all instances of
Given a string, I want to retrieve a string that is in between the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.