Regex to match first three octets of IP address [closed] - regex

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
The community reviewed whether to reopen this question 5 months ago and left it closed:
Original close reason(s) were not resolved
Improve this question
I see this regular expression will match an IP address:
\b(?:\d{1,3}\.){3}\d{1,3}\b
How do I change it so it will only match the first three octets of an IP address?
So, provided with 1.2.3.4, it will only match 1.2.3.

First of all, the regex you supplied is incorrect. It will match an IP adress, but also something like 192a168.1f12. The reason is because . is a special character in regular expressions which equates to any character. Escape this with a \ so you get \b(?:\d{1,3}\.){3}\d{1,3}\b and you have an IP address regex.
Then, analyze the regex - it consists of two main parts - the part for the a.b.c. part where a, b and c are numbers with one to three digits (which is technically not an IP address since it should go to 255, but it's close enough) and then there's the last one to three digit number part.
Notice that the first part ends with a {3}, specifying it should appear 3 times. You want it to appear only 2 times, followed by the same second part so you get \b(?:\d{1,3}\.){2}\d{1,3}\b.
This should be enough for you, but depending on your requirements, you could also make sure that the first three octets are followed by the last octet (but not match it) by using a positive-lookahead.
Also, you don't really need a non-matching group so you can simplify your regex to this:
\b(\d{1,3}\.){2}\d{1,3}\b

Do this:
address: \b(?:\d{1,3}.){2}\d{1,3}\b
Allright so what does the above mean? well \d matches a digit, the {1,3} means that there should be 1-3 digits no more, no less. the dot . actually matches any character (including dots, more correctly would be \.) Since we have this statement inside of parentheses, and here I have to confess I have no clue which language you're using, although i suspect it's PHP so I don't really know what the ?: does. But the {2} after those says that the pattern will repeat twice, then the last \d{1,3} matches the third octet.

Related

Are regular expressions (0*1*)* and (0 + 1)* same? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I was solving exercise problems in my textbook, and I wondered that whether (0*1*)* and (0 + 1)* are same. I think they are same, but I have no idea how to prove it. Are they same regular expressions?
No, they are not the same:
0*1* matches any number of zeroes (including none) followed by any number of ones (including none). This is then repeated any number of times (including none) via ()*
0 + 1 matches a mandatory single zero, then at least one or more spaces, then one more space, then a mandatory single one. Again, this is repeated any number of times via ()* like before.
So, the two match very different things the first one will match input like 000000 or 1111 because it has (essentially) an optional zero or one, so an input that only has a single of these is correct but not according to the second regex. While the second one will match input that has at least two or more spaces between the mandatory 0 and 1 characters but the first regex will reject that, since it does not allow spaces.
No they aren't. The first on will for example match 0011, but the second one won't.
You can check how they behave in: https://regex101.com/
I think you meant (0+1)*instead of (0 + 1)* (mind the spaces, they are meaningful when dealing with regex).
To answer your question, no. They are not the same.
The + quantifier means "one or more" (it does not mean "or" like you might think), so the (0+1)* regex will match 01 but not 10, whereas the (0*1*)* regex will match both.

regex to match string not containg at least X characters [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am looking for a regex to match a string that doesn't contain at least three characters [a-zA-Z]
Valid Strings:
abc12345
asd
abc12321!!
Invalid Strings:
aa!
111!!!a
!!!!!!b
I would use:
(?:[^a-zA-Z]*[a-zA-Z]){3,}[^a-zA-Z]*
You can find an explaination of this regex here.
matches:
abc12345
asd
abc12321!!
a85ug
83nj%#8n2
doesn't match:
aa!
111!!!a
!!!!!!b
a59&*#g9
You can check whether the following regex matches the string:
[a-zA-Z]{3,}
If it doesn't match then the string is invalid.
Basically the {3,} part of the regex says the the previous character class should be matched at least three times. It's pretty flexible in that you can specify a minimum number and a maximum number of times required for a match like so: {MIN,MAX} and if you omit either MIN or MAX then only the value specified is used (i.e. {3,} means at least 3, whereas {,3} would mean at most 3)

Regex for two digit number followed by . for find and replace in vim [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I want to find out following string pattern in vim and replace it with some thing else. Can you please tell me regex for the same.
1.
11.
20.
21.
99.
basically one / two digits followed by dot.
I think you could do something like the following (I'm not very experienced with VI so there might be a better way)
:%s/\d\+\./MyString/gc
So that's essentially using \d\+\. to search for numbers appearing one or more times followed by a ..
MyString is your replacement string.
:%s is the substitute command, :s would just search the current line.
/gc looks for the match as many times as it appears on the line (g), and asks for confirmation before each replacement (c).
Tried this?
[^0-9][0-9][0-9]\.
Or have you tried it and it didn't work?
It has an issue though of three digits and a dot, i.e. "123." will also be captured
The regex for 1 or 2 digits followed by a dot is:
\<\d\d\?\.
The "word boundary" \< precludes 3 digits (and a dot), which would be allowed without it (the last two digits of a 3-digit number would match).
To replace using this tegex in vi:
s/\<\d\d\?\./foo/g

Regex remove spaces after 2 letters [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
What is the regex for no whitespaces after the first 2 letters? For example, the string starts with AB then is followed by an undetermined amount of numbers and/or letters but it cannot have any spaces.
(^[A][B][\S])
^[a-zA-Z]{2}\S+$
Edit live on Debuggex
^ denotes the start of the line
[a-zA-Z]{2} denotes exactly 2 letters
\S any character besides white spaces
+ one or more.
$ denotes end of string.
please read more about regex here and use debugexx.com to experiment.
It looks like you're using Python, so I'll just assume that.
^\w\w\S*
That's 2 word characters at the start of the line, followed by 0 or more non-whitespace characters.
Not sure what platform you're using, but you can try the following:
^[\d\w]{2}[^\s]*$
Should do the trick.
The [\d\w]{2} represents 2 letters or digits and then the [^\s] means no white space for zero or more characters afterwards. The caret and dollar sign notation force this to happen at the beginning of the string.
EDIT: Although the title of this question is remove white space your question seems to just ask for the regex for 2 letter/digit characters with no white space afterwards, which is what I answered. If you are looking for how to make a lookahead ignore my answer.

Regex to get string till it hits a comma [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
Let's say I have a string like this: "1367,14,243,540"(will always have 4 number and only numbers, no decimal places and always separated by comma)
How should the regex look like that would allow me to pick/filter out/return lets say 243 from the string?
here is your regex if you insist on a regex /\d+/g (g here is for multiple selections in js, use matches with Microsoft framework) though you can use split (example using js):
var v='123,333,445,67';
console.log('split:');
console.log(v.split(',').map(function(n){return parseInt(n);}));
console.log('\nregex:');
console.log(v.match(/\d+/g).map(function(n){return parseInt(n);}));
jsfiddle
the numbers will be returned in an array, you can use the index to access the desired one, let's say 2.
note: split is faster than regex, you can test the difference in performance using jsperf.com
Edit: For those who are interested in the performance difference, check this link.
note2: map here is just for parsing the strings into integers, you can remove it if you want to keep them as strings.
try
^([[:digit:]]+,){2}([[:digit:]]+)
your desired number is in capture group #2.
As one of the comments says, you shouldn't really use a regex in this case. Always try to use the appropriate tool for the job, and in this case the regex is HUGE overkill.
Your problem is solved easily as this
$sourceString = "1367,14,243,540";
$numbers = explode(",", $sourceString);
$neededNumber = $numbers[2];
You just need to describe your string:
^(\d+,){2}(\d+)
"From the start, number followed by comma appears two times, then another number."
You can pick the number of the second group, i.e. \2 or $2.