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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T17:48:13+00:00 2026-06-16T17:48:13+00:00

I would like to get strings inside [square] [brackets] and also those @like @this

  • 0

I would like to get strings inside [square] [brackets] and also those @like @this. The following works for the [tags] but not the @users.

$query = "[tag1] [tag2] @user1 @user2 ignoreme";

// check for tags e.g. [tag1] [tag2]
preg_match_all("/\[([^\]]+)\]/", $query, $tags);

// check for users e.g. @username
preg_match_all("/\B@[^\B]+/", $query, $users);

print_r($tags);
print_r($users);

Output:

(
    [0] => Array
        (
            [0] => [tag1]
            [1] => [tag2]
        )
        [1] => Array
        (
            [0] => tag1
            [1] => tag2
        )
    )
Array
(
    [0] => Array
        (
            [0] => @user1 @user2 ignoreme
        )
    )

The tag preg_match is working but the @user one just grabs everything after it (even ignoring the second @user2).

  • 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-16T17:48:14+00:00Added an answer on June 16, 2026 at 5:48 pm

    If you look at this explanation: http://regex101.com/r/fH4lG4 you’ll find out why your regex is broken.

    You’re matching everything BUT the literal B.

    What you should be doing is either [^@]+, [^@ ]+ or \S+

    I hope this helps 🙂

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

Sidebar

Related Questions

I have a string Like this: MyText (1,151) I would like to get with
I would like to send a html string with a GET request like this
Suppose I would like to match for the following pipe-delimited strings: std::string re(a|*|c); std::string
My ContentValues object has string keys, and I would like to get a String[]
I would like to get the address string of the current user location. Is
Could somebody help me.I would like to get substring of string by ant.For example
I would like to get text width of a string before an application starts.
I have a string in C# and would like to get text from specific
public class MyClass { public string MyProperty{ get; set; } Now, I would like
I would like to get numbers of comments for each posts using mysql. I

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.