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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T23:31:24+00:00 2026-05-14T23:31:24+00:00

I want to perform a regex using grouping. I am only interested in the

  • 0

I want to perform a regex using grouping. I am only interested in the grouping, its all I want returned. Is this possible?

$haystack = '<a href="/foo.php">Go To Foo</a>';
$needle = '/href="(.*)">/';
preg_match($needle,$haystack,$matches);
print_r($matches);

//Outputs
//Array ( [0] => href="/foo.php">  [1] => /foo.php ) 

//I want:
//Array ( [0] => /foo.php ) 
  • 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-14T23:31:24+00:00Added an answer on May 14, 2026 at 11:31 pm

    Actually this is possible with lookarounds. Instead of:

    href="(.*)">
    

    You want

    (?<=href=").*(?=">)
    

    Now this will match (and therefore capture into group 0) any .* that is preceded by href=" and followed by ">. Note that I highly suspect that you really need .*? instead, i.e. reluctant instead of greedy.

    ---A--Z---A--Z----
       ^^^^^^^^^^^
          A.*Z
    

    In any case, it looks like PHP’s preg is PCRE, so it should support lookarounds.

    regular-expressions.info links

    • Lookaround
    • Flavor comparison
      • PHP’s preg functions implement the PCRE flavor.
      • PCRE:
        • (?=regex) (positive lookahead): YES
        • (?<=text) (positive lookbehind): fixed + alternation

    Demonstration

    <?php
    
    $haystack = '<a href="/foo.php">Go To Foo</a><a href="/bar.php">Go To Bar</a>';
    $needle = '/(?<=href=").*?(?=">)/';
    preg_match_all($needle,$haystack,$matches);
    print_r($matches);     
    
    ?>
    

    Running this on ideone.com produces:

    Array
    (
        [0] => Array
            (
                [0] => /foo.php
                [1] => /bar.php
            )
    
    )
    

    Related questions

    These are mostly Java, but the regex part covers using lookarounds/assertions:

    • Java split is eating my characters
    • Can you use zero-width matching regex in String split?
    • How do I convert CamelCase into human-readable names in Java?
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to perform changes in the code which was developed using iOS 3.0,
I am trying to perform a regex query using PyMongo against a MongoDB server.
Suppose I want perform a simulation using the following function : fn1 <- function(N)
Can someone help me rewrite this regex to be non-exponential? I'm using perl to
i want to perform some test by using Json . I am using google
I want to perform a system-wide hook (using SetWindowHook) on a 64bit operating system.
How is it possible to perform a matching of string, just like regex ?
I want to perform a hidden HTTP-GET request from MS-access, as simple as possible,
I want to perform some AJAX style data retrieval using the Java Play Framework
I want to perform an operation on a array returned from an ActiveRecord query.

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.