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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T13:25:51+00:00 2026-06-05T13:25:51+00:00

I want to match two different string and output should come in $1 and

  • 0

I want to match two different string and output should come in $1 and $2,
According to me in this example, if $a is ‘xy abc’, then $1 should be ‘xy abc’ and $2 should ‘abc’, but ‘abc’ part is coming in $3.
Can you please help me to writing a regex in that $1 should have whole string and $2 should
have second part.
I am using perl 5.8.5.

my @data=('abc xy','xy abc');
foreach my $a ( @data) {
    print "\nPattern= $a\n";
    if($a=~/(abc (xy)|xy (abc))/) {
        print "\nMatch: \$1>$1< \$2>$2< \$3>$3<\n";
    }
}

Output:

perl test_reg.pl

Pattern= abc xy

Match: $1>abc xy< $2>xy< $3><

Pattern= xy abc

Match: $1>xy abc< $2>< $3>abc<
  • 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-05T13:25:55+00:00Added an answer on June 5, 2026 at 1:25 pm

    Because only one of captures $2 and $3 can be defined, you can write

    foreach my $item ( @data) {
    
      print "\nPattern= $item\n";
    
      if ($item=~/(abc (xy)|xy (abc))/) {
        printf "Match: whole>%s< part>%s<\n", $1, $2 || $3;
      }
    }
    

    which gives the output

    Pattern= abc xy
    Match: whole>abc xy< part>xy<
    
    Pattern= xy abc
    Match: whole>xy abc< part>abc<
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to match and modify part of a string if following conditions are
I want to match links like <a href=mailto:my@email.com>foo</a> , but this doesn't work only
Suppose you want a Blog with two different layouts. One layout should look like
I want to match a date in the format day/month/year. where day is two
I want to match against Strings such as AhKs & AdKs (i.e. two cards
I want to do a functional like pattern match to get the first two
I want to match strings like those below. abc|q:1,f:2 cba|q:1,f:awd2,t:3awd,h:gr I am using php
I want to match all lines that have any uppercase characters in them but
In my application which uses spring security I want to define two different areas
I want to perform update operation by joining two different tables. My tables are

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.