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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T12:34:11+00:00 2026-05-30T12:34:11+00:00

I need to match everything between ‘ [~ ‘ and ‘ ~] ‘ tags.

  • 0

I need to match everything between ‘[~‘ and ‘~]‘ tags.

Tried to write a lot of regex patterns but couldn’t find correct one:

  1. #\[~(.*)~]# – this returns everything between first occurrence of [~ and last occurrence of ~].
  2. #\[~([^~]*)~]# – this works fine if there are no ~ symbol inside tags.

I understand that (.*) captures everything and ([^~]*) captures everything until it finds ~ character but I cant make it to capture everything until it finds ~] pair (any byte excepting ~] pair is possible inside tags including single ~ character). In other words, I dont know how to make negation against the pair of characters.

This is possible example:

Simple [example~]: [~here I can face both, ‘~’ and ‘]’ characters~] or another
example [~~~~~~[ABC]~~~~~~]. 

After preg_match_all() against regex I expect resulting array like this:

array(2) {
  [0]=>
  string(44) "here I can face both, '~' and "]" characters"
  [1]=>
  string(14) "~~~~~[ABC]~~~~~"
}

Note: Input string may contain binary data (00-FF).

Just to mention (for certain people here), I’ve already checked out all related Q/A + hundreds of Google search results.

  • 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-30T12:34:13+00:00Added an answer on May 30, 2026 at 12:34 pm

    * is greedy, so it takes as much as it can. You can make it non-greedy (add a ?) which should solve your issue.

    #\[~(.*?)~]#
    

    The following website has a good description and explains it in more detail: Repetition with Star and Plus.

    preg_match deals with binary strings pretty well, the . matches any character which reads as byte if you’re in the standard mode (non-utf8) – as you are.


    Simplified example for explanation:

     aab ::  a*  -> aa
    

    Matches first an empty string, then a, then aa and then aab does not match so the last match aa is taken and returned. As you can see the engine had first internally three valid matches: empty string, a and aa. The last one wins in greedy-mode.

     aab ::  a*? -> (empty string)
    

    Is at first position. Needs 0 or more a non-greedy. First position is zero or more a, so matches an empty string and returns. The first one wins in non-greedy-mode.

    For UTF-8 strings, use the u modifier (PCRE8): #.*#u – . matches any UTF-8 character (which can be one or more bytes).

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

Sidebar

Related Questions

I need a regular expression able to match everything but a string starting with
I need to write a regular expression that will match everything in the string
What reg expression patten to I need to match everything between {{ and }}
I need to match abreviations, where a dot means everything that starts with, like:
I need help with a RegEx problem: I want to find occurences of two
I have a comma delimited list of numbers. I need to match everything up
How can I use regex to find everything except for data within div with
Need some help with regex matching please. I'm trying to match a double quoted
I need a regex that will match the US phone in a slightly different
I need to match input strings (URLs) against a large set (anywhere from 1k-250k)

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.