what Regex Expression do we need to use in this scenario - regex

what Regex Expression do we need to use in this scenario
Iam looking to have Regular Expression where we need to validate the user that he cannot enter a value of "ZERO"
is this correct "regex":/^0$/,
Valid responses are:
1,2,3,4,5,6,7,8,9,10,20,
Invalid Responses are
0

No, and it is rather complicated and clumsy to write a regex saying "but not x". Try to avoid regexes in that case.

Often times, it's helpful to list a set of valid and invalid responses.
Valid:
joe
bob
zero
amy0
s0nny
0mar
Invalid:
0
If this is the case, then the easiest solution is to say username == "0", and skip the regular expression. If, rather, you need to make sure that the 0 can not be at the front of any string, then something like username[0] == '0' would work.
On the other hand, if you want to make sure that there are no 0 characters anywhere in the string, then I would use something like not username.contains('0').
I guess what I'm getting at is that this doesn't sound like a problem that demands a regular expression.

Try using this...
^(0.+|[^0].*)$
You can test it here...
http://regexpal.com

Related

regular expression for string with dot del

I am looking for a regular expression for the following string:
SQL Err 100 on \TEST1.$PROD01.TEST.XYZ562
I want to search for anything with TEST*.$PROD*.TEST.XYZ*.
Can anyone help with the regular expression?
Basic effort was required, you could just tried it yourself and, I believe, find an answer on your own! For future needs, try site regex101.com, it helps to work with regulars.
So, you need to find a string containing TEST, then something, then . then $PROD value, again anything, and .TEST.XYZ, and finally anything, where $PROD is, as I understand, a variable without special characters (as [](){}.^\).
So, you get that:
TEST[^.]*\.<$PROD value should go here>[^.]*\.TEST\.XYZ
That should be enough. You will need to supstitude variable $PROD on your own, but I don't know your language, so I can't help with that part. Maybe something like this:
"TEST[^.]*\." + $PROD + "[^.]*\.TEST\.XYZ"

How to check for NOT this number in SoapUI Assertion

I am writing some Soap UI tests and am trying to figure out if there is a way with regular expressions to check for a string that does not contain a specific number. In this one case I want to make sure that when I get a response that my recordCount field DOES NOT contain 0. I thought this might be easier but while I can see a way to check for a set of numbers the regular expression for not this doesn't seem to work. Probably only detects characters and not numbers.
My XML contains this:
<recordCount>0</recordCount>
What I want is something like
recordCount>[^0]
so I can make sure recordCount shows up in the response, but also check that at least the first number it finds is not a 0. Is there any way to do this?
Edit: Using SiKing's answer I just used the NotContains to look for recordCount>0 ; this covers the couple of cases where I don't look for specific data only how many records are returned and in those cases it just needs to be more than 0
Why does it have to be regular expression?
You can use either of the following XPath assertions, for all of which the expected result is false:
//*:recordCount = 0
exists(//*:recordCount[text()='0'])
Using regular expressions you could do something like that:
(?s).*<recordCount>[^0]</recordCount>(?s).*
When using regular expressions in an assertion in SoapUI, you have to take whitespace and line breaks into account. In the example code (?s).* works as a wildcard that includes all whitespace and line breaks.

Validate incomplete Regex

Let's say we have a Regex, in my case it's one I found to match UK car registration plates:
^([A-Z]{3}\s?(\d{3}|\d{2}|d{1})\s?[A-Z])|([A-Z]\s?(\d{3}|\d{2}|\d{1})\s?[A-Z]{3})|(([A-HK-PRSVWY][A-HJ-PR-Y])\s?([0][2-9]|[1-9][0-9])\s?[A-HJ-PR-Z]{3})
A typical UK car registration is
HG53CAY
This is matched correctly by the regex, but what i'd like to do is find a way to match any prefix substring of this, so the following would all be valid:
H, HG, HG5, HG53, HG53C, HG53CA, HG53CAY
Is there a suggested way to achieve this?
Firstly I'd rewrite your regexp to look like this:
^([A-Z]{3}\s?(\d{1,3})\s?[A-Z])|([A-Z]\s?(\d{1,3})\s?[A-Z]{3})|(([A-HK-PRSVWY][A-HJ-PR-Y])\s?([0][2-9]|[1-9][0-9])\s?[A-HJ-PR-Z]{3})
as the \d{3}|\d{2}|d{1} parts make no sense and should be written \d{1,3}.
Rewriting the regexp like
^([A-Z]{0,3}\s?(\d{0,3})\s?[A-Z]?)|([A-Z]\s?(\d{0,3})\s?[A-Z]{0,3})|(([A-HK-PRSVWY][A-HJ-PR-Y]?)\s?([0]?[2-9]?|[1-9]?[0-9]?)\s?[A-HJ-PR-Z]{0,3})
should have the desired effect of allowing matching of only the beginning of a registration, but unfortunately it's no longer guaranteed that the full registration will be a valid one, as I had to make most characters optional.
You could possibly try something like this
^(([A-Z]{3})|[A-Z]{1,2}$)\s?((\d{1,3})|$))...
to make it require either that each part is complete, or that it is incomplete but followed by "end of string", represented by the $ in the regexp.

How to only match before the first dot?

I have the following regex.
^((?!example).)*$#Subdomain is reserved (example).
I would like to validate <subdomain>.example.org. However, since the domain name contains example, a match is occurring.
The validation should not match when the address is www.example.org
The validation should match when the address is example.example.org
Looks like you're missing the escape character from the period
^(example)\..*$
should work
It seems that a simple
^example\.
is enough. Or use string methods, depending on your language:
url.indexOf('example.') === 0
If input such as example.org is also possible, you can use
^example\..+\.
to force the appearance of two dots. But this would still fail for example.co.uk. It depends on your input.
A simple way might be to break it up into two:
^.+\.example\.org$
^(www)?\.example\.org$
If 1) matches and 2) does not, it's a subdomain of example.org; otherwise, it's not. (Although www technically is a subdomain, but you understand.)

Reg Ex - not allowing zero

Excuse my ignorance. My knowledge of regular expressions is extremely limited.
I have the following regular expression on a regular expression validator.
^(\d{1,3},?(\d{3},?){0,2}\d{3}|\d{1,3})$
It currently allows zero. I need to update it so that it does not allow a value of 0.
[Edit: Just to clarify, I would like it to exclude a value of zero rather than a singular "0" - thanks]
Any help would be greatly appreciated.
Thanks
zaps
You may be looking for something like this:
^[1-9]\d{0,2}(\d*|(,\d{3})*)$
0 is allowed by the second part of your regex. Change it to:
^(\d{1,3},?(\d{3},?){0,2}\d{3}|[1-9]\d{0,2})$
It makes sure that the first digit is non zero, when the total number of digits are less than or equal to three.
The regex still allows patterns like 000,000,000 and 000,123 To fix that you can change the first part of the regex to:
^([1-9]\d{0,2},?(\d{3},?){0,2}\d{3}|[1-9]\d{0,2})$
Or rewrite it as
^[1-9]\d{0,2}(,?\d{3}){0,3}$
This still allows 123,456789 and 123456,789. Let us change it to:
^[1-9]\d{0,2}(?:(,\d{3}){0,3}|(\d{3}){0,3})$
This will allow 123,456,789 and 123456789 but not 123,456789 or 123456,789