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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T01:28:56+00:00 2026-06-09T01:28:56+00:00

I have a pattern in a file as follows which can/cannot span over multiple

  • 0

I have a pattern in a file as follows which can/cannot span over multiple lines :

 abcd25
 ef_gh
 ( fg*_h
 hj_b*
 hj ) {

What I have tried :

perl -nle ‘print while m/^\s*(\w+)\s+(\w+?)\s*(([\w-0-9,* \s]))\s{/gm’

I dont know what the flags mean here but all I did was write a regex for the pattern and insert it in the pattern space .This matches well if the the pattern is in a single line as :

abcd25 ef_gh ( fg*_h hj_b* hj ) {

But fails exclusively in the multiline case !

I started with perl yesterday but the syntax is way too confusing . So , as suggested by one of our fellow SO mate ,I wrote a regex and inserted it in the code provided by him .

I hope a perl monk can help me in this case . Alternative solutions are welcome .

Input file :

 abcd25
 ef_gh
 ( fg*_h
 hj_b*
 hj ) {

 abcd25
 ef_gh
 fg*_h
 hj_b*
 hj ) {

 jhijdsiokdù ()lmolmlxjk;
 abcd25 ef_gh ( fg*_h hj_b* hj ) {

Expected output :

 abcd25
 ef_gh
 ( fg*_h
 hj_b*
 hj ) {
 abcd25 ef_gh ( fg*_h hj_b* hj ) {

The input file can have multiple patterns which coincides with the start and end pattern of the required pattern.
Thanks in advance for the replies.

  • 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-09T01:28:58+00:00Added an answer on June 9, 2026 at 1:28 am

    The regex does not match even the single line. What do you think the double parentheses do?

    You probably wanted

    m/^\s*(\w+)\s+(\w+?)\s*\([\w0-9,*\s]+\)\s{/gm
    

    Update: The specification has changed. The regex has (almost) not, but you have to change the code slightly:

    perl -0777 -nle 'print "$1\n" while m/^\s*(\w+\s+\w+?\s*\([\w0-9,*\s]+\)\s{)/gm'
    

    Another update:

    Explanation:

    • The switches are described in perlrun: zero, n, l, e
    • The regex can be auto-explained by YAPE::Regex::Explain

      perl -MYAPE::Regex::Explain -e 'print YAPE::Regex::Explain->new(qr/^\s*(\w+\s+\w+?\s*\([\w0-9,*\s]+\)\s{)/)->explain'
      The regular expression:
      
      (?-imsx:^\s*(\w+\s+\w+?\s*\([\w0-9,*\s]+\)\s{))
      
      matches as follows:
      
      NODE                     EXPLANATION
      ----------------------------------------------------------------------
      (?-imsx:                 group, but do not capture (case-sensitive)
                               (with ^ and $ matching normally) (with . not
                               matching \n) (matching whitespace and #
                               normally):
      ----------------------------------------------------------------------
        ^                        the beginning of the string
      ----------------------------------------------------------------------
        \s*                      whitespace (\n, \r, \t, \f, and " ") (0 or
                                 more times (matching the most amount
                                 possible))
      ----------------------------------------------------------------------
        (                        group and capture to \1:
      ----------------------------------------------------------------------
          \w+                      word characters (a-z, A-Z, 0-9, _) (1 or
                                   more times (matching the most amount
                                   possible))
      ----------------------------------------------------------------------
          \s+                      whitespace (\n, \r, \t, \f, and " ") (1
                                   or more times (matching the most amount
                                   possible))
      ----------------------------------------------------------------------
          \w+?                     word characters (a-z, A-Z, 0-9, _) (1 or
                                   more times (matching the least amount
                                   possible))
      ----------------------------------------------------------------------
          \s*                      whitespace (\n, \r, \t, \f, and " ") (0
                                   or more times (matching the most amount
                                   possible))
      ----------------------------------------------------------------------
          \(                       '('
      ----------------------------------------------------------------------
          [\w0-9,*\s]+             any character of: word characters (a-z,
                                   A-Z, 0-9, _), '0' to '9', ',', '*',
                                   whitespace (\n, \r, \t, \f, and " ") (1
                                   or more times (matching the most amount
                                   possible))
      ----------------------------------------------------------------------
          \)                       ')'
      ----------------------------------------------------------------------
          \s                       whitespace (\n, \r, \t, \f, and " ")
      ----------------------------------------------------------------------
          {                        '{'
      ----------------------------------------------------------------------
        )                        end of \1
      ----------------------------------------------------------------------
      )                        end of grouping
      ----------------------------------------------------------------------
      
      • The /gm switches are explained in perlre
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a file containing some lines of code followed by a string pattern.
I have a file containing records delimited by the pattern /#matchee/. These records are
I currently have the following Get-ChildItem -recurse -include file.ext | Select-String -pattern c: |
I have an xml file in which I have to find patterns and put
I have a file as follows 2.54 Ghz val 2.53 Ghz val1 1.6 Ghz
I have a generated file with thousands of lines like the following: CODE,XXX,DATE,20101201,TIME,070400,CONDITION_CODES,LTXT,PRICE,999.0000,QUANTITY,100,TSN,1510000001 Some
I have on HTML file in which I have inserted the custom tags like
I am using a web application framework (Symfony 1.3.6), which follows the MVC pattern.
I have both PHP and HTML in one file, following the pattern: -HTML1 -PHP1
I have a line in my web.config which is as follows <clientDependency loggerType=xxx.ClientDependencies.Logger,StrattonWebShared version=144>

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.