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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T13:18:41+00:00 2026-05-24T13:18:41+00:00

I have a regex for IP address validation, but I need a regex for

  • 0

I have a regex for IP address validation, but I need a regex for prefix validation, where the expected form is “IP Address/Prefix”.

The conditions are:

  1. The prefix value should not be greater than 128

  2. The prefix value should be divisible by 4.

Could anyone please help me to create a regex for prefix validation?

  • 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-24T13:18:42+00:00Added an answer on May 24, 2026 at 1:18 pm

    AFAIK you can’t do calculations using regex (e.g. number % 4 == 0 ?). Thus you’d have to use a pattern that gets all possible string combinations.

    Try that one: \b[048]\b|\b[13579][26]\b|\b[2468][048]\b|\b1[02][048]\b|\b11[26]\b

    • \b[048]\b matches 0, 4 and 8
    • \b[13579][26]\b matches 12, 16, 32, 26 etc.
    • \b[2468][048]\b matches 20, 24, 28, 40, 44, 48 etc.
    • \b1[02][048]\b matches 100, 104, 108, 120, 124, 128
    • \b11[26]\b matches 112 and 116

    Note the \b which defines the whole word (in your case the prefix/suffix) must match the pattern. Without it, 136 might match [13579][26], for example.

    Edit: to allow leading zeros change the pattern to: \b0{0,2}[048]\b|\b0?[13579][26]\b|\b0?[2468][048]\b|\b1[02][048]\b|\b11[26]\b (note that 0{0,2} could also be written as 0?0?).

    Edit 2: you might get rid of the \b if you split the ip address and only have a string containing the prefix/suffix. If you then call matches(...) you should be fine without the \b.

    Pattern for matches(...) calls (no \b, allows leading zeros):
    0{0,2}[048]|0?[13579][26]|0?[2468][048]|1[02][048]|11[26]

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

Sidebar

Related Questions

I have this regex working but now need to allow numbers without the decimal
I have a regex call that I need help with. I haven't posted my
I have a RegEx that is working for me but I don't know WHY
I have an input field taking an email address: <h:inputText value=#{register.user.email} required=true /> How
I am making an form on my website but I have an problem with
Does anyone have a php class, or regex to parse an address into components?
I have a collection of strings that need to be parsed (using RegEx patterns)
Issue is resolved now... I have a simple web form where i need to
So I have an IP address as a string. I have this regex (\d{1-3})\.(\d{1-3})\.(\d{1-3})\.(\d{1-3})
Looking for hostname validation regex. Regular expression to match DNS hostname or IP Address?

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.