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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T10:26:45+00:00 2026-05-26T10:26:45+00:00

I have a regular expression for checking whether a string is zip/postal code or

  • 0

I have a regular expression for checking whether a string is zip/postal code or not. But I would really like to also to be able to extract that from a full address (or, if possible, any string).

Here is my current regular expression:

/^((\d{5}-\d{4})|(\d{5})|([a-zA-Z]\d[a-zA-Z]\s\d[a-zA-Z]\d)|([a-zA-Z]\d[a-zA-Z]\d[a-zA-Z]\d))$/

If necessary I’m willing to settle for a function (I’m checking with PHP) but I’d rather the regexp do the work if possible.

  • 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-26T10:26:46+00:00Added an answer on May 26, 2026 at 10:26 am

    preg_match, which I assume you’re already using when you’re checking a string against your regular expression, also gives you back the actual text that matched your pattern.

    preg_match($regex, $input, $matches);
    echo $matches[0];
    

    The third argument is filled with the results of trying to match the regex against your input. $matches[0] will contain text that matched the whole pattern, while higher indexes will contain text that matched against capturing subpatterns (the parts of the pattern enclosed in parentheses).

    However, in your case, you’ve enclosed your pattern with the start-of-input ^ and end-of-input $ characters, which means that any matches must include the entire input string (or an entire line in multiline mode). You’d have to get rid of the ^ and $ before trying to use this pattern to extract a postal code from a larger string.

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

Sidebar

Related Questions

I have a regular expression, say /url.com\/([A-Za-z]+)\.html/ , and I would like to replace
I have a regular expression, defined in a verbatim C# string type, like so:
I have a regular expression like this (much simplified): ^(ab)*$ And am matching against
Anybody have a regular expression to replace the following code: <a href=originalLink>hi</a> with: <a
I have some regular expression code that grabs the data between the title tags
I have regular expression like this: error 1|error 2|error 3|error 4|error 5 about 80
I have regular expression which is checking for at least one character or number:
I have a regular expression that strips everything but letters. numbers, and periods. How
I have a regular expression to match filenames which look like this: name -
I have a regular expression to match a persons name. So far I have

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.