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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T19:37:20+00:00 2026-05-13T19:37:20+00:00

i’m having some troubles with regular expressions in ruby. I need to categorize some

  • 0

i’m having some troubles with regular expressions in ruby. I need to categorize some files that have the first line followed by two newlines, like in this example:

GIOVIANA

Si scrivono miliardi di poesie
sulla terra ma in Giove è ben diverso.
Neppure una se ne scrive. E certo
la scienza dei gioviani è altra cosa.
Che cosa sia non si sa. È assodato
che la parola uomo lassù desta
ilarità.

Empty lines can occur in other position of the file as well as double empty lines. I tried the following regexp (and many others)

/\A.*\n\n/

but i’m not getting the desired result.

I’ll explain my whole project too, in case someone has a better idea on how to do it. I need to automatically markup textual structures in a plain text. I can do it fairly well with regular expressions to identify lines, sentences, and so on, but i can’t tell my program this:

if the first line is followed by two newlines it is a tile, so mark it up with the title markup and go on on the third line
if the first line is not followed by two newlines then the poem does not have a title, markup the first line as a title and then markup all the poem (including the first line)

in the first case the desired result is

[poem}[title}GIOVIANA{title]

[line}[sentence}Si scrivono miliardi di poesie{line]
[line}sulla terra ma in Giove è ben diverso.{sentence]{line]
[line}[sentence}Neppure una se ne scrive.{sentence][sentence} E certo{line]
[line}la scienza dei gioviani è altra cosa.{sentence]{line]
[line}[sentence}Che cosa sia non si sa.{sentence] [sentence}È assodato{line]
[line}che la parola uomo lassù desta{line]
[line}ilarità.{sentence]{line]
{poem]

in a poem without a title like

Ora sia il tuo passo
più cauto: a un tiro di sasso
di qui ti si prepara
una più rara scena.

the desired result is

[poem}[title}[line}[sentence}Ora sia il tuo passo{line]{title]
[line}più cauto: a un tiro di sasso{line]
[line}di qui ti si prepara{line]
[line}una più rara scena.{line]{sentence]{poem]

Thanks

  • 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-13T19:37:21+00:00Added an answer on May 13, 2026 at 7:37 pm

    You don’t need (sophisticated) regular expressions for that, just write a parser:

    lines = string.split("\r\n")
    or
    lines = File.readlines(fname),

    then something like this:

    IN_SENTENCE=false
    if lines[1] =~ /\w+/
     puts "[poem}[title}[line}[sentence}#{lines[0].strip}{line]{title]"
     IN_SENTENCE=true
     start = 1
    else
     puts "[poem}[title}#{lines[0].strip}{title]"
     start = 2
    end
    lines[start..lines.size].each do |line|
      #process line
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 361k
  • Answers 361k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer It really is a matter of opinion. Many web services,… May 14, 2026 at 2:50 pm
  • Editorial Team
    Editorial Team added an answer The way to get the dropped element is to have… May 14, 2026 at 2:50 pm
  • Editorial Team
    Editorial Team added an answer I've converted code to SL4, installed new plugin (4.0.50401.0) and… May 14, 2026 at 2:50 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.