I got this regular expression:
192\.168\.[1|2|5|20]\.[0-9]{1,3}
192.168.2.123 -> OK
192.168.5.123 -> OK
192.168.20.123 -> Error
I want to accept just value: 1 - 2 - 5 - 20 on X --> 192.168.X.122
(the rest of regular expression is correct, i just got the problem when i try to get value 20)
I can't reproduce your observations, but here is a pattern which should meet your requirements:
192\.168\.(?:1|2|5|20)\.122
Demo
It looks like you were confounding character classes, which are characters inside square brackets, with an alternation, which are different patterns of text, one of which needs to match.
This
[1|2|5|20]
actually says to match the numbers 0, 1, 2, 5 or pipe. If you want to match any of these numbers, then use an alternation:
(1|2|5|20)
Related
My regex currently looks like this
\b(19|20)\d{2}\b[- :][VW][0-5]{1}(?(?=[5])[0-2]{1}|[0-9]{1})
It doesn't quite do what I want as I'm trying to get this part
(?(?=[5])[0-2]{1}|[0-9]{1})
to say "If the previous number was 5 then you may only choose between 0-2, and if it's another number 0-4 then choosing between 0-9 is allowed
Currently it allowes 00-59 with an exclusion of 05,15,25,35 etc.
Essentially I want it to look like this for example 2016-W25.
You need to replace [5] with a positive lookbehind (?<=5) in order to check a char to the left of the current location:
\b(19|20)\d{2}[- :][VW][0-5](?(?=(?<=5))[0-2]|[0-9])
^^^^^
See the regex demo
Also, you may get rid of the conditional pattern at all using a mere alternation group:
\b(19|20)\d{2}[- :][VW](?:[0-4][0-9]|5[0-2])
^^^^^^^^^^^^^^^^^^^^^
See this regex demo
The (?:[0-4][0-9]|5[0-2]) matches either a digit from 0 to 4 and then any digit (see [0-4][0-9]), or (see |) a 5 followed with 0, 1 or 2 (see 5[0-2]).
NOTE: Since the number of weeks can amount to 53, the [0-2] at the end might be replaced with [0-3] to also match 53 values.
I came across the regular expression not containing 101 as follows:
0∗1∗0∗+(1+00+000)∗+(0+1+0+)∗
I was unable to understand how the author come up with this regex. So I just thought of string which did not contain 101:
01000100
I seems that above string will not be matched by above regex. But I was unsure. So tried translating to equivalent pcre regex on regex101.com, but failed there too (as it can be seen my regex does not even matches string containing single 1.
Whats wrong with my translation? Is above regex indeed correct? If not what will be the correct regex?
Here is a bit shorter expression ^0*(1|00+)*0*$
https://www.regex101.com/r/gG3wP5/1
Explanation:
(1|00+)* we can mix zeroes and ones as long as zeroes occur in groups
^0*...0*$ we can have as many zeroes as we want in prefix/suffix
Direct translation of the original regexp would be like
^(0*1*0*|(1|00|000)*|(0+1+0+)*)$
Update
This seems like artificially complicated version of the above regexp:
(1|00|000)* is the same as (1|00+)*
it is almost the solution, but it does not match strings 0, 01.., and ..10
0*1*0* doesn't match strings with 101 inside, but matches 0 and some of 01.., and ..10
we still need to match those of 01.., and ..10 which have 0 & 1 mixed inside, e.g. 01001.. or ..10010
(0+1+0+)* matches some of the remaining cases but there are still some valid strings unmatched
e.g. 10010 is the shortest string that is not matched by all of the cases.
So, this solution is overly complicated and not complete.
read the explanation in the right side tab in regex101 it tells you what your regex does( I think you misunderstood what list operator does) , inside a list operator ( [ ) , the other characters such as ( won't be metacharacters anymore so the expression [(0*1*0*)[1(00)(000)] will be equivalent to [01()*[] which means it matches 0 or 1 or ( or ) or [
The correct translation of the regular expression 0∗1∗0∗+(1+00+000)∗+(0+1+0+)∗
will be as follows:
^((?:0*1*0*)|(?:1|00|000)*|(?:0+1+0+)*)$
regex101
Debuggex Demo
What your regex [(0*1*0*)[1(00)(000)]*(0+1+0+)*] does:
[(0*1*0*)[1(00)(000)]* -> matches any of characters 0,(,),*,[ zero or more times followed by
(0+1+0+)* --> matches the pattern 0+1+0+ 0 or more times followed by
] --> matches the character ]
so you expression is equivalent to
[([)01](0+1+0+)*] which is not a regular expression to match strings that do not contain 101
0* 1* ( (00+000)* 1*)* (ε+0)
i think this expression covers all cases because --
any number apart from 1 can be broken into constituent 2's and 3's i.e. any number n=2*i+3*j. So there can be any number of 0's between 2 consecutive 1's apart from one 0.Hence, 101 cannot be obtained.
ε+0 for expressions ending in one 0.
The RE for language not containing 101 as sub-string can also be written as (0*1*00)*.0*.1*.0*
This may me a smaller one then what you are using. Try to make use of this.
Regular Expression I got (0+10)1. (looks simple :P)
I just considered all cases to make this.
you consider two 1's we have to end up with continuous 1's
case 1: 11111111111111...
case 2: 0000000011111111111111...(once we take two 1's we cant accept 0's so one and only chance is to continue with 1's)
if you consider only one 1 which was followed by 0 So, no issue and after one 1 we can have any number of 0's.
case 3: 00000000 10100100010000100000100000 1111111111
=>(0*+10*)1
final answer (0+10)1.
Thanks for your patience.
I am currently writing a small texteditor. With this texteditor users are able to create small scripts for a very simple scripting engine.
For a better overview I want to highlight codeblocks with the same command like GoTo(x,y) or Draw(x,y).
To achieve this I want to use Regular Expresions (I am already using it to highlight other things like variables)
Here is my Expression (I know it's very ugly):
/(?<!GoTo|Draw|Example)(^(?:GoTo|Draw|Example)\(.+\)*?$)+(?!GoTo|Draw|Example)/gm
The "logic":
(?< !GoTo|Draw|Example) : Negative Lookbehind. No GoTo/Draw/Example command in the line before (inserted a space to avoid rendering problems)
(^(?:GoTo|Draw|Example)(.+)*?$) now macth GoTo/Draw/Example() until line end (even match a comment)
"+" find last pattern min. one times
UNTIL in the next line does not contain GoTo/Draw/Example (negative lookahead)
(for testing at regex101.com, finaly I need this for vb.net)
It matches the following:
-Code- -result- execpted result
GoTo(5656) -> MATCH 1 -> MATCH 1
sdsd
GoTo(sdsd) --comment -> MATCH 2 -> MATCH 2
GoTo(23329); -> MATCH 3 -> MATCH 2
Test()
GoTo(12) -> MATCH 4 -> MATCH 3
LALA
Draw(23) -> MATCH 5 -> MATCH 4
Draw(24) -> MATCH 6 -> MATCH 4
Draw(25) -> MATCH 7 -> MATCH 4
But what I want to achieve is, that the complete "blocks" of the same command are matched. In this case Match 2 & 4 and Match 5 & 6 & 7 should be one match.
Image: Group example:
Tested with http://regex101.com/, the programming lanuage is vb.net.
The expression you are looking for is
((?:^GoTo.+\n?)+)|((?:Draw.+\n?)+)
You can see this at work here
The key here is the use of \n to match "everything including the end of the line" so you can match across multiple lines. Also note the use of ?: to get non-capturing inner groups (which are repeated) so we don't end up with inner and outer matches (we only want "the whole block" matched). Finally, the | separates an entire block of Draw from an entire block of GoTo. Obviously if you have other keywords, you can repeat for those as well.
I want to match a 3-digit number only from a webpage.
So for example if webpage has number 1 599-+ (white space between 1 and 5 and -+ signs after). I only want to capture/match numbers between 0 and 599-+ and nothing else.
My regex is: regex(?:^|(?:[^\d\s]\s*))([0-5]\d\d-+) but this one also matches "i 1599-+"
or regex(\^[0-5]?[0-9]?[0-9]-+$) doesnt work either...
A solution would be to use this regular expression with a non capturing group matching either the start of the string or something that's not a digit (with a little more verbosity due to space handling) :
(?:^|(?:[^\d\s]\s*))([0-5]\d\d)
Examples (in javascript as you didn't specify a language) :
"1 599".match(/(?:^|(?:[^\d\s]\s*))([0-5]\d\d)/) => null
"a sentence with 1 599 inside".match(/(?:^|(?:[^\d\s]\s*))([0-5]\d\d)/) => null
"another with 599".match(/(?:^|(?:[^\d\s]\s*))([0-5]\d\d)/) => ["h 599", "599"]
"599 at the start".match(/(?:^|(?:[^\d\s]\s*))([0-5]\d\d)/) => ["599", "599"]
(desired group is at index 1)
I hope this is needed for you.Try it, if it is not fulfilling.Write a little more description.
/^[0-5]?[0-9]?[0-9]$/.test("599");
From the above I understood and developed this, I think this is what you needed.
/^[0-5]?[0-9]?[0-9][\+|-]?$/.test("599");
In the above regex I made + - as optional and it'll check for presence of any one sign.
If you want in the order of -+ then try this
/^[0-5]?[0-9]?[0-9][\-][\+]$/.test("99-+"); .
Okay #user3214294
I am beginner and have some problems with regexp.
Input text is : something idUser=123654; nick="Tom" something
I need extract value of idUser -> 123456
I try this:
//idUser is already 8 digits number
MatchCollection matchsID = Regex.Matches(pk.html, #"\bidUser=(\w{8})\b");
Text = matchsID[1].Value;
but on output i get idUser=123654, I need only number
The second problem is with nick="Tom", how can I get only text Tom from this expresion.
you don't show your output code, where you get the group from your match collection.
Hint: you will need group 1 and not group 0 if you want to have only what is in the parentheses.
.*?idUser=([0-9]+).*?
That regex should work for you :o)
Here's a pattern that should work:
\bidUser=(\d{3,8})\b|\bnick="(\w+)"
Given the input string:
something idUser=123654; nick="Tom" something
This yields 2 matches (as seen on rubular.com):
First match is User=123654, group 1 captures 123654
Second match is nick="Tom", group 2 captures Tom
Some variations:
In .NET regex, you can also use named groups for better readability.
If nick always appears after idUser, you can match the two at once instead of using alternation as above.
I've used {3,8} repetition to show how to match at least 3 and at most 8 digits.
API links
Match.Groups property
This is how you get what individual groups captured in a match
Use look-around
(?<=idUser=)\d{1,8}(?=(;|$))
To fix length of digits to 6, use (?<=idUser=)\d{6}(?=($|;))