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 9163549
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T14:28:26+00:00 2026-06-17T14:28:26+00:00

In the following content samples, I wrapped the lines to make it easier to

  • 0

In the following content samples, I wrapped the lines to make it easier to read on Stackoverflow (so you don’t have to scroll to the right to look at the examples).

Content A:

"Lorem Ipsum\r\n
[img]http://example.org/first.jpg[/img]\r\n
[img]http://example.org/second.jpg[/img]\r\n
more lorem ipsum ..."

Content B:

"Lorem Ipsum\r\n
[img caption="Sample caption"]http://example.org/third.jpg[/img]
[img]http://example.org/fourth.jpg[/img]"

Content C:

"Lorem Ipsum [img]http://example.org/fifth.jpg[/img]\r\n
more lorem ipsum\r\n\r\n
[img caption="Some other caption"]http://example.org[/img]"

What I’ve tried:

content.match(/\[img\]([^<>]*)\[\/img\]/imu)
return example: "[img]...[/img]\r\n[img]...[/img]
content.scan(/\[img\]([^<>]*)\[\/img\]/imu)
return example: "...[/img]\r\n[img]..."

What I would like to accomplish when running the scan/match/regex solution over the above 3 Content Examples is to get every occurence of [img]...[/img] and [img caption="?"]...[/img] and put it in an array for later use.

Array
  1 : A : [img]http://example.org/first.jpg[/img]
  2 : A : [img]http://example.org/second.jpg[/img]
  3 : B : [img caption="Sample caption"]http://example.org/third.jpg[/img]
  4 : B : [img]http://example.org/fourth.jpg[/img]
  5 : C : [img]http://example.org/fifth.jpg[/img]
  6 : C : [img caption="Some other caption"]http://example.org[/img]

It would also be helpful to limit the “stripped content” to only where there is an open and closign tag, meaning when there is a [img] / [img caption="?"] and a missing [/img] afterwards, to ignore it.

I’ve read the http://www.ruby-doc.org/core-1.9.3/String.html up and down but can’t find anything that seem to work for this.

Update:

So I figured that this:

\[img([^<>]*)\]([^<>]*)\[\/img\]

will find either:

[img]something[/img]

and:

[img caption="something"]something[/img]

Now I just need to know how to catch every occurence inside the different contents. I can always just get it from the first to the last [img][/img] tags, so when there is other Lorem Ipsum in between it will get grabbed too.

  • 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-06-17T14:28:27+00:00Added an answer on June 17, 2026 at 2:28 pm

    You can use /\[img(?:\s+caption=".+")?\].+?\[\/img\]/ to scan the documents:

    regex = /\[img(?:\s+caption=".+")?\].+?\[\/img\]/
    
    text = <<EOT
    Lorem Ipsum
    [img]http://example.org/first.jpg[/img]
    [img]http://example.org/second.jpg[/img]
    more lorem ipsum ...
    
    Content B:
    
    Lorem Ipsum
    [img caption="Sample caption"]http://example.org/third.jpg[/img]
    [img]http://example.org/fourth.jpg[/img]
    
    Content C:
    
    Lorem Ipsum [img]http://example.org/fifth.jpg[/img]
    more lorem ipsum
    
    [img caption="Some other caption"]http://example.org[/img]
    EOT
    
    array = text.scan(regex)
    puts array
    

    Which generates:

    [img]http://example.org/first.jpg[/img]
    [img]http://example.org/second.jpg[/img]
    [img caption="Sample caption"]http://example.org/third.jpg[/img]
    [img]http://example.org/fourth.jpg[/img]
    [img]http://example.org/fifth.jpg[/img]
    [img caption="Some other caption"]http://example.org[/img]
    

    If you want to ignore the tags and only grab the content, change the regexp to:

    regex = /\[img(?:\s+caption=".+")?\](.+?)\[\/img\]/
    

    Running again with that change returns:

    http://example.org/first.jpg
    http://example.org/second.jpg
    http://example.org/third.jpg
    http://example.org/fourth.jpg
    http://example.org/fifth.jpg
    http://example.org
    

    (Rubular proof)

    If you need to look for different tags, you can generate an “OR” list easily:

    Regexp.union(%w[foo img bar])
    => /foo|img|bar/
    

    If you need to make sure that “magic” characters are escaped beforehand:

    Regexp.union(%w[foo img bar].map{ |s| Regexp.escape(s) })
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Having read around it appears this should be possible. I have the following content
Let's say I have the following content: Lorem Ipsum is simply dummy text of
I have a file containing the following content 1000 line in the following format:
I have an .htaccess file with following content: AddHandler x-httpd-php5 .php For some reason,
I have an html page with the following content: [...] <tr><td class=n><a href=play-1.0.1.zip>play-1.0.1.zip</a></td></tr> <tr><td
I have a xPage with following content: <xe:restService id=restService preventDojoStore=false> <xe:this.service> <xe:viewJsonService viewName=vwChartData contentType=text/json>
I have a txt file named 'a.txt' with the following content: Hi=Python Now, I
I have a file test.xml with the following content : <body> <content> <p>Lorem ipsum
I have a file ( myenv.sh ) with the following content export MYVAR=HELLO And
I have the following two structs: The problem is the sizeof(Content) returns 160. 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.