In Asp.net for RegularExpressionValidator, I need to regex validation for don't allow html tag in textbox having only < or > or &#.
Also, I can validate separately for < or > with this regex ([^<>])*
and separately for "&#" with regex ^((?!&#).)*$
But not able to validate both together. So please suggest me how to fix that problem.
Thanks.
Here is a possible solution:
^((?!(&#|>|<)).)*$
Try it online.
This is your provided ^((?!&#).)*$ modified by changing &# to (&#|>|<).
Related
I need to modify this regex to find multiple group matches:
(?:--)(?<key>[^\s=]+)(?:(?<assign> *[ =] *)(?! --)(?<value>"[^"]*"|\S+))?
In Java:
"(?:--)(?<key>[^\\s=]+)(?:(?<assign> *[ =] *)(?! --)(?<value>\"[^\"]*\"|\\S+))?"
This matches the following correctly:
--key=value
--key=--value
--key value
--flag
--key="--value"
--key "--value"
--key=value --foo=bar
--key=value --foo=bar --flag
But it fails if --flag comes before any other options:
--key=value --flag --foo=bar
I've been trying to modify the negative lookahead between the assign and value capture groups without success so far. The value captured for flag ends up being --foo=bar instead of null.
Any expert recommendations on how to solve this?
I managed to fix the regex. The website https://regexr.com/ was invaluable.
The fixed regex is:
(?<prefix>--)(?<key>[^\s=]+)(?:(?! --)(?<assign> *[ =] *)(?! --)(?<value>"[^"]*"|\S+))?
Here's the Java class and unit test:
https://gist.github.com/kirklund/845baf340a1999a57db9e59e6ba40ce0
I want to exclude a specific subdomain from a regex.
I have searched and tryed out different regex. But non worked for me.
The normal regex looks like this:
https?:\/\/((localhost(\:\d+)?)|([a-z\-\.]*\.)?(gaga.ch|gugus.ch))
To exclude a subdomain with name admin in gugus.ch I added:(^(?!.*admin).*)
So the whole regex looks like:
https?:\/\/((localhost(\:\d+)?)|([a-z\-\.]*\.)?(gaga.ch|(^(?!.*admin).*)gugus.ch))
So it should let through http://www.gugus.ch
But NOT http://admin.gugus.ch
This does not work. What I'am doing wrong?
thx Mike
Try this regex:
https?://((localhost(:\d+)?)|([a-z.-]*\.)?(gaga\.ch|(?<!\badmin\.)gugus\.ch)
(?<!\badmin\.) is a negative lookbehind to fail the match if gugus.ch is preceded by admin.
I have the following requirement where in I need to do few things only if the given string ends in "Y" or "Years" or "YEARS".
I tried doing it using regex like this.
String text=1.5Y;
if(Pattern.matches("Y$",text) || Pattern.matches("YEARS$",text) || Pattern.matches("Years",text))
{
//do
}
However this is getting failed.
Can someone point me where I have gone wrong or suggest me any other feasible method.
EDIT:
Thanks.That helps.
Finally I have used "(?i)^.*Y(ears)?$| (?i)^.*M(onths)?$".
But I want to make more changes to make it perfect.
Let's say I have many strings.
Ideally only strings like 1.5Y or 0.5-3.5Y or 2.5/2.5-4.5Y should pass if check.
It can be number of years(Ex:2.5y) or the period of years(2.5-3.5y) or the no of years/period of years(Ex.2.5/3.5-4.5Y) nothing more.
More Examples:
--------------
Y -should fail;
MY - should fail;
1.5CY - should fail;
1.5Y-2.5Y should fail;
1.5-2.5Y should pass;
1.5Y/2.5-3.5Y should fail;
1.5/2.5-3.5Y should pass;
You don't need a regex here:
if(text.endsWith("Y") || ...)
matches method attempts to match full input so use:
^.*Y$
for your first pattern.
btw you can use a single regex for all 3 cases:
if (text.matches( "(?i)^.*Y(ears)?$" ) ) {...}
(?i) does ignore case match.
.*(?:Y|YEARS|Years)$
You can directly use this .Match matches from beginning.So yours is failing.
You can simply use the regex pattern:
if (Pattern.matches(".*(Y|YEARS|Years)$",text)) {/*do something*/}
/((?!0)\d+|0)(.\d+)?(?:years|year|y)/gi
https://regex101.com/r/gJ6xD2/2
var text = "1.6y 1.5years 1year 1.5h";
text.match(/((?!0)\d+|0)(\.\d+)?(?:years|year|y)/gi);
Result["1.6y", "1.5years", "1year"]
(?=^(0\.\d+|[1-9](?:\d+)?(?:\.\d+)?)(?:(\s+)?[\/-](\s+)?(?:0\.\d+|[1-9](?:\d+)?(?:\.\d+)?))*(?:\s+)?(?:y(?:(ea)?rs|ears?)?|m(?:onths?)?)$).*
https://regex101.com/r/kL7rQ1/3
Only thing I wasn't sure "2.3 - 4 / 6.2 y" format is acceptable or not, so I've included it.
I need to write regex in java to match domain and subdomain(.domain.com).
Regex should return true for
domain.com
m.domain.com
abc.domain.com
www.domain.com
but returns false for
abcdomain.com
1domain.com
I try to match domain.com and and if preceding character is present then it must be .
I tried various options but it is failing in one or other test cases.
(^|.*?\.)domain\.com
Try this. See demo.
http://regex101.com/r/lB2sH2/1
Try this:
(\.|^)domain.com$
The first part means that there should be a . or nothing
and the $ means, "ends with"
You can try:
(^|\.)domain\.com$
but Java mostly handles only full-line matches, so:
(.+\.)?domain\.com
or you can use the .endWith() method in Java code:
if (domain.equals("domain.com") || domain.endsWith(".domain.com")) {
// do something...
}
I think you want something like this,
(?:\\w+\\.?)?domain\\.com
DEMO
try this regex
\bdomain\.com$
http://rubular.com/r/QG0FtVWtm6
If you don't know what "domain.com" is going to be, this regex below should give you just the subdomain of whatever domain you are looking for. Matches your specifications, including domains that look like abc.net
([a-z]+)(?=\.[a-z]+\.)
DEMO
I have the following REGEX that I'm serving up to java via an xml file.
[a-zA-Z -\(\) \-]+
This regex is used to validate server side and client side (via javascript) and works pretty well at allowing only alphabetic content and a few other characters...
My problem is that it will also allow zero lenth strings / empty through.
Does anyone have a simple and yet elegant solution to this?
I already tried...
[a-zA-Z -\(\) \-]{1,}+
but that didn;t seem to work.
Cheers!
UPDATE FOLLOWING INVESTIGATION
It appears the code I provided does in fact work...
String inputStr = " ";
String pattern = "[a-zA-Z -\\(\\) \\-]+";
boolean patternMatched = java.util.regex.Pattern.matches(pattern, inputStr);
if ( patternMatched ){
out.println("Pattern MATCHED");
}else{
out.println("NOT MATCHED");
}
After looking at this more closely I think the problem may well be within the logic of some of my java bean coding... It appears the regex is dropped out at the point where the string parse should take place, thereby allowing empty strings to be submitted... And also any other string... EEJIT that I am...
Cheers for the help in peer reviewing my initial stupid though....!
Have you tried this:
[a-zA-Z -\(\) \-]+