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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T09:32:15+00:00 2026-05-25T09:32:15+00:00

Since, I’m new in regular expressions; I want to make a regular expression to

  • 0

Since, I’m new in regular expressions; I want to make a regular expression to select two consecutive words.

For example when i give this phrase: “Hello people #RegularExpression sucks!”

It has to return these couple words:

-Hello people

-people #RegularExpression

-#RegularExpression sucks!

I tried this /\w\s\w/i but it did not work 🙁

  • 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-25T09:32:15+00:00Added an answer on May 25, 2026 at 9:32 am
    $s = "Hello people #RegularExpression sucks!";
    preg_match_all('~(?=(\S+\s+\S+))\S+\s+~', $s, $matches);
    print_r($matches[1]);
    

    output:

    Array
    (
        [0] => Hello people
        [1] => people #RegularExpression
        [2] => #RegularExpression sucks!
    )
    

    explanation:

    \S+ matches one or more non-whitespace characters. Your \w was incorrect for two reasons: it only only matches one character; and it only matches a so-called word character (equivalent to [A-Za-z0-9_]). Adding the + to your \s wasn’t necessary in this test case, but there’s no reason not to add it, and extra whitespace does have a way of sneaking into text in the real world. (But be sure and add +, not *; there must be at least one whitespace character in there.)

    (?=...) is a positive lookahead. You use them to check whether it’s possible to match the enclosed subexpression at the current match position, without advancing the match position. Then, typically, you go ahead and match a different subexpression, not in a lookahead.

    Here’s the tricky bit: Although the characters matched by the lookahead subexpression are not consumed, any capturing groups in the subexpression work as usual. The lookahead in my regex, (?=(\S+\s+\S+)) matches and captures the next two-word sequence. Then (assuming the lookahead succeeded) \S+\s+ matches in the normal way, setting the match position correctly for the next attempt.

    This technique should work in any regex flavor that supports capturing groups and lookaheads. That includes PHP as well as all the other major languages (Perl, JavaScript, .NET, Python, Java…). The technique for accessing only the contents of the first capturing group from each match varies wildly from one language to the next, but PHP makes it easy, with $matches[1].

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Since I started this new webshop for a friend to launch, which still in
since they have same rendering engine, this problem shows in both. it works great
Since this morning my local install of EE 1.6.8 is failing to load the
Since I started writing this question, I think I figured out the answers to
Since the getElementsByTagName() function is new (DOM-1?) I wanted another more reliable method to
Since I moved a PHP app to a new server (PHP/5.3.8 running as Apache
Since I'm using Java 1.4.2, this means that I cannot use Java's implementation of
Since C+++ allows function overloading, can we overload main() ? For example, int main(const
since this morning I keep getting this error when trying to launch my MVC
Since I am new to using Unity 2.0, I need some pointer or suggestion

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.