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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T08:50:41+00:00 2026-05-14T08:50:41+00:00

I’m trying to to convert an existing PHP regular expression to apply to a

  • 0

I’m trying to to convert an existing PHP regular expression to apply to a slightly different style of document.

Here’s the original style of the document:

**FOODS - TYPE A** 
___________________________________ 
**PRODUCT** 
1) Mi Pueblito Queso Fresco Authentic Mexican Style Fresh Cheese; 
2) La Fe String Cheese 
**CODE** 
Sell by date going back to February 1, 2009 

And the successfully-running PHP Regex match code that only returns “true” if the line is surrounded by asterisks, and stores each side of the “-” as $m[1] and $m[2], respectively.

 if ( preg_match('#^\*\*([^-]+)(?:-(.*))?\*\*$#', $line, $m) ) { 
    // only for **header - subheader** $m[2] is set. 
    if ( isset($m[2]) ) { 
      return array(TYPE_HEADER, array(trim($m[1]), trim($m[2]))); 
    } 
    else { 
      return array(TYPE_KEY, array($m[1])); 
    } 
  } 

So, for line 1: $m[1] = “FOODS” AND $m[2] = “TYPE A”;
Line 2 would be skipped; Line 3: $m[1] = “PRODUCT”, etc.

The question: How would I re-write the above regex match if the headers did not have the asterisks, but still was all-caps, and was at least 4 characters long? For example:

FOODS - TYPE A 
___________________________________ 
PRODUCT
1) Mi Pueblito Queso Fresco Authentic Mexican Style Fresh Cheese; 
2) La Fe String Cheese 
CODE
Sell by date going back to February 1, 2009 

Thank you.

  • 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-14T08:50:42+00:00Added an answer on May 14, 2026 at 8:50 am

    Along the lines of (don’t forget the “u” flag for Unicode regexes):

    ^(?:\*\*)?(?=[^*]{4,})(\p{Lu}+)(?:\s*-\s*(\p{Lu}+))?(?:\*\*)?\s*$
    
    ^               # start of line
    (?:\*\*)?       # two stars, optional
    (?=[^*]{4,})    # followed by at least 4 non-star characters
    (\p{Lu}+)       # group 1, Unicode upper case letters
    (?:             # start no capture group
      \s*-\s*       #   space*, dash, space*
      (\p{Lu}+)     #   group 2, Inicode upper case letters
    )?              # end no capture group, make optional
    (?:\*\*)?       # two stars, optional
    \s*             # optional trailing spaces
    $               # end of line
    

    EDIT: Simplified, as per the comments:

    ^(?=[A-Z ]{4,})([A-Z ]+)(?:-([A-Z ]+))?\s*$
    
    ^               # start of line
    (?=[A-Z -]{4,}) # followed by at least 4 upper case characters, spaces or dashes
    ([A-Z ]+)       # group 1, upper case letters or space
    (?:             # start no capture group
      -             #   a dash
      ([A-Z ]+)     #   group 2, upper case letters or space
    )?              # end no capture group, make optional
    \s*             # optional trailing spaces
    $               # end of line
    

    Contents of groups 1 and 2 must be trimmed before use.

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

Sidebar

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.