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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T07:39:24+00:00 2026-06-02T07:39:24+00:00

Hi all regex newbie here., I am trying to write a regex that validates

  • 0

Hi all regex newbie here.,

I am trying to write a regex that validates IPv6 addresses. I haven’t added the port part yet, I want to get the address portion working first.

This is what I have so far:

\\[?(?:[:0-9A-Fa-f]+)\\]?

This makes the opening and closing brackets optional. But as you can see they are independently optional. Is there a way with regexes to require either both opening and closing brackets, or neither?

  • 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-02T07:39:26+00:00Added an answer on June 2, 2026 at 7:39 am

    Regex is a great tool for string pattern matching, but you’ve hit upon one of its weakness here. Matching pairs of brackets can get tricky.

    In more complex situations — ie when trying to read program code — the solution is not to use regex at all, but to use a proper code parser.

    Even fairly simple cases can be tricky to answer because the regex syntax involved can be quite hairy, and also because with more advanced regex features like this, different implementations may use different syntax. This question might provide some hints as to how to go about it, and the different syntaxes you might use.

    However in your case, the problem isn’t too complex, and a fairly simple solution presents itself: simply match the whole expression with both brackets required or the whole expression with neither bracket. In other words, repeat the main part of your expression twice, once with brackets and once without, and a pipe for or between them:

    \(\[(?:[:0-9A-Fa-f]+)\])|(?:[:0-9A-Fa-f]+)\
    

    Hope that helps.

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

Sidebar

Related Questions

Regex newbie here. There's a (broken) plugin for the forum software I use that
Here's my regex code: preg_match_all('/background[-image]*:[\s]*url\([|\']+(.*)[|\']+\)/', $css, $matches, PREG_SET_ORDER); It looks for CSS that looks
I created this regex to match all words that start with @ in my
What's the single regex that enables me to capture all the text that goes
I've been trying all day with no luck to regex replace the new line
HI I want regex option that find website links like here : www.yahoo.com yahoo.com
is it possible to have a regex that will find all regex condition statements?
I'm a newbie to regex and I'm trying to come up with a regular
Im trying to craft a regex that only returns <link> tag hrefs Why does
(\.|dot) This regex matches all .s and dots but it's also puts them into

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.