LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-09-2019, 04:33 AM   #1
grzeslaw
Member
 
Registered: Nov 2008
Posts: 111

Rep: Reputation: 24
Question IP+MASK regexp


Hi,

I am trying to write regexp to validate IP address and netmask. Sadly my regexp still catch wrong netmask:

Code:
  IP_ADDR_REGEXP="(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])(\/(3[0-2]|[1-2][0-9]|[0-9]))"
Could anyone help me with fixing that? Netmask should be 2integers long and be in range 1-32..
 
Old 07-09-2019, 08:05 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,792

Rep: Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306
regex101.com can be useful to construct regex
without example and the used language hard to say anything.
 
Old 07-09-2019, 08:43 AM   #3
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,725

Rep: Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211
Mayhaps this will help:
https://www.regextester.com/22
 
Old 07-09-2019, 11:05 AM   #4
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,263
Blog Entries: 24

Rep: Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194
It would be helpful if you would tell us more precisely what qualifies as valid. It is not possible for others to derive the conditions of validity from your regex when you have already said it does not produce the desired result.

Please tell us more precisely what networks you want to match, with an example of matching and non matching IP.

Please review the Site FAQ for guidance in posting your questions and general forum usage. Especially, read the link in that page, How To Ask Questions The Smart Way. The more effort you put into understanding your problem and framing your questions, the better others can help!
 
Old 07-10-2019, 06:57 AM   #5
grzeslaw
Member
 
Registered: Nov 2008
Posts: 111

Original Poster
Rep: Reputation: 24
Thanks for your comments guys, appreciate that!
Honestly, I want to match all available local and external networks with mask up to 32.
for example:
Code:
10.0.10.0/24
200.211.15.0/28
212.5.0.0/19
172.20.0.0/20
144.44.0.4/32
 
Old 07-10-2019, 08:02 AM   #6
Kenhelm
Member
 
Registered: Mar 2008
Location: N. W. England
Distribution: Mandriva
Posts: 360

Rep: Reputation: 170Reputation: 170
The regex needs a '$' end-of-line anchor to stop it matching substrings.
......|[0-9]))$"
Code:
echo /{0..255} | tr ' ' '\n' |     # /0 to /255
grep '/[0-9]$'                     # Without the $ it would match all of /0 to /255
/0
/1
/2
/3
/4
/5
/6
/7
/8
/9
 
1 members found this post helpful.
Old 07-10-2019, 09:50 AM   #7
grzeslaw
Member
 
Registered: Nov 2008
Posts: 111

Original Poster
Rep: Reputation: 24
Quote:
Originally Posted by Kenhelm View Post
The regex needs a '$' end-of-line anchor to stop it matching substrings.
......|[0-9]))$"
Code:
echo /{0..255} | tr ' ' '\n' |     # /0 to /255
grep '/[0-9]$'                     # Without the $ it would match all of /0 to /255
/0
/1
/2
/3
/4
/5
/6
/7
/8
/9
And the winner is.... Kenhelm!
Thanks a lot mate!
That's what I miss - just one magic dollar!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
postfix regexp question wijnands Linux - Newbie 1 06-03-2004 06:19 AM
perl regexp problem raven Programming 4 03-21-2004 11:49 PM
regexp question rytrom Linux - Newbie 3 09-01-2003 12:50 PM
validating a surname - regexp fu chr15t0 Programming 2 06-20-2003 05:55 AM
Regexp stumper lackluster Programming 2 11-02-2002 12:31 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:04 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration