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

  • Home
  • SEARCH
  • 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 6831727
In Process

The Archive Base Latest Questions

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

I have chunks of strings within square brackets, like this: [p1 text1/label1] [p2 text2/label2]

  • 0

I have chunks of strings within square brackets, like this:

[p1 text1/label1] [p2 text2/label2] [p3 text3/label3] [...

and so on.

What’s inside each chunk isn’t important. But sometimes there are stray chunks of text that are NOT surrounded by square brackets. For example:

[p1 text1/label1] [p2 text2/label2] textX/labelX  [p3 text3/label3] [...] textY/labelY textZ/labelZ [...]

I thought I had this solved fine with regex in perl until I realized that I have only catered to the cases where there is a single stray text at the beginning, the middle, or the end of the text, but not where we might have two stray cases together. (like the Y and Z chunks above).

So I realized that regular expressions in perl only catch the first matching pattern? How could the above problem be solved then?

Edit:

The problem is to ensure that all should be surrounded by brackets. Square brackets are never recursive. When surrounding a phrase with brackets, the p-value depends on the “label” value. For eg, if a stray unbracketed phrase is

li/IN

then it should turn into:

[PP li/IN]

I guess it is a mix but the only way I can think of solving the bigger problem I’m working on is to turn all of them into bracketed phrases, so the handling is easier. So I’ve got it working if an unbracketed phrase happens at the beginning, middle and end, but not if two or more happen together.

I basically used a different regex for each position (beginning, middle and end). The one that catches an unbracketed phrase in the middle looks like this:

$data =~ s/\] (text)#\/label \[/\] \[selected-p-value $1#\/label\] \[/g;

So what I’m doing is just noticing that if a ] comes before and after the text/label pattern, then this one doesn’t have brackets. I do something similar for the others too. But I guess this is incredibly un-generic. My regex isn’t great!

  • 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-26T22:46:18+00:00Added an answer on May 26, 2026 at 10:46 pm

    Actually you can solve this using “only” regex :

    #!/usr/bin/perl
    
    use strict;
    use warnings;
    
    $_ = "[p1 text1/label1] [p2 text2/label2] textX/labelX  [p3 text3/label3] [...] textY/labelY textZ/labelZ [...]";
    
    s{ ([^\s[]+)|(\[(?:[^[]*)\])     }
     { if( defined $2){ $2 } elsif(defined $1)
        { 
           if($1 =~ m!(.*(?<=/)(.*))!)
           {
             if($2 eq 'labelX')
             {
                "[PP $1]";
             }
             elsif($2 eq 'labelY')
             {
                "[BLA $1]";
             }
             elsif($2 eq 'labelZ')
             {
                "[FOO $1]";
             }
           }
        }
     }xge;
    
     print;
    

    Output :

    [p1 text1/label1] [p2 text2/label2] [PP textX/labelX]  [p3 text3/label3] [...] [BLA textY/labelY] [FOO textZ/labelZ] [...]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If I have a chunk of code like this: .hover( function () { hoverState($(#navbar
I have some code that roughly does this inside of a GAE worker task:
I have one string, that looks like this: a[abcdefghi,2,3,jklmnopqr] The beginning a is fixed
I have a multipart string like this: String Y = part1 part2 part3 part4;
I have this string containing a large chunk of html and am trying to
I have several chunks of PCM audio (G.711) in my C++ application. I would
In a desktop application needing some serious re-factoring, I have several chunks of code
I have an 8 byte message where the differing chunks of the message are
I have an application which will use WCF to serve up various chunks of
I have a server application that, in rare occasions, can allocate large chunks of

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.