How to make this regular expression Pattern case insensitive - for list of words [duplicate] - regex

Given this regular expression: "^[0-9]*\s*(lbs|kg|kgs)$" how do I make it case insensitive? I am trying to use this in a .net regular expression validator, so I need to specify case insensitivity in the pattern.
I can not use the RegexOptions programatically because I am specifying the regular expression in a RegularExpressionValidator

I found out.
Case sensitive: ^[0-9]\s(lbs|kg|kgs)$
Case insensitive: (?i:^[0-9]\s(lbs|kg|kgs)$)
I believe that this is specific to the .NET implementation of regular expressions. So if you use this in the RegularExpressionValidator you have to turn off client side validation because the javascript regex parser will not recognize the ?i token.

Here is an alternative using a CustomValidator, when really needing case-insensitivity server-side and client-side; and the the Upper/lower [A-Za-z] char approach is too much.
This blends the various other answers, using the server-side RegEx object and client-side JavaScript syntax.
CustomValidator:
<asp:CustomValidator ID="cvWeight" runat="server" ControlToValidate="txtWeight"
OnServerValidate="cvWeight_Validate" ClientValidationFunction="cvWeight_Validate"
ValidateEmptyText="true" Text="*" ErrorMessage="Invalid entry." />
Code behind:
protected void cvWeight_Validate(object sender, ServerValidateEventArgs args)
{
Regex re = new Regex(#"^[0-9]*\s*(lbs|kg|kgs)$", RegexOptions.IgnoreCase);
args.IsValid = re.IsMatch(args.Value);
}
Client-side validation function:
function cvWeight_Validate(sender, args) {
var reWeight = /^[0-9]*\s*(lbs|kg|kgs)$/i;
args.IsValid = reWeight.test(args);
}
This is working fine for me, except when using a ValidationSummary. On the client-side validation, the error * shows, but I can't get the error message to display in the summary. The summary only displays when submitted. I think it's supposed to display; I have a mix of update panels and legacy code, which may be problems.

Can we make Regex case-insensitive in C#? : Yes
Set option inline via (?i) pattern construct or via RegexOptions.IgnoreCase param
Can we make Regex case-insensitive in JavaScript? : Yes
Set flag via /pattern/flags syntax or the insensitive flag /REGEX/i
(Aside) Can we make Regex case-insensitive in HTML5 Pattern Attribute? : No
As Chris R. Timmons points out on RegularExpressionValidator doesn't ignore case:
There is no property in the RegularExpressionValidator control that
allows regex options to be set.
If your control does both client-side and server-side validation, the
regex must use a subset of regular expression syntax that both
JS and .NET can execute. In this case, to make a regex ignore
case it is necessary to use a character class construct like [a-zA-Z]
to match both upper and lower case characters.
If your validation is done on the server-side only, you can use the
more powerful .NET regular expression syntax. In this case, you can
place the (?i) option at the beginning of the regex to tell it to
ignore case.
So, if you want to use the out of the box validator, you're stuck with Geoff's solution of using character sets like this: [aA]

Easiest here is to just modify the regex to
^[0-9]*\s*([lL][bB][sS]|[kK][gG][sS]?)$
It's awful to read, but it will work fine.

Related

Regex in Google Analytics Core Reporting API

The API is here: https://developers.google.com/analytics/devguides/reporting/core/v3/reference#filters
I'm trying to match this:
/mysite/some-other-stuff
but NOT:
/Mysite/some-other-stuff
(note the capital M v. lower case...I want just the lower case.)
The API has a filter and I'm trying this:
ga:pagePath=~(.*)(m)ysite(.*)
This matches both the lower case and upper case versions. Any suggestions on what might work? I've tested this with a few online regex testers and it seems like it should work, so I'm thinking there's something specific about the syntax that Google Analytics wants.
Thanks!
It is written in the docs: Case sensitivity — Regular expression matching is case-insensitive.
You may use an inline modifier group (?-i:...) where you explicitly tell the regex engine to match in a case sensitive way:
(?-i:.*mysite.*)
or even
(?-i:mysite)
since GA regex does not require a full string match.

Regular Expression is working for php not for JavaScript

This is my expression:
^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])[[:graph:]]{6,25}$
I have tested it on http://regex101.com/
Its working for string DeepakManwal1 when there is php selected but does not work when javascript is selected. I don't know what is the exact reason.
I want to use this expression for password validation where there should be at-least one uppercase letter and one numeric character.
Here is fiddle http://jsfiddle.net/j7rmj44h/
It is because of the POSIX class [:graph:] — you could change it to the equivalent [\x21-\x7E]. Also, you need to remove the quotations around your pattern according to your fiddle.
var re = /^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])[\x21-\x7E]{6,25}$/
Fiddle
AFAIK, javascript doesn't understand POSIX.
Have a try with:
^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])\S{6,25}$
Different regex engines have different capabilities. Only the simplest of regex can be shared across implementations.
If I recall correctly [:graph:] like specification of character classes is not supported in JS.

How can I allow lower case post codes in this regular expression?

This is the regular expression I use to validate UK post codes.
It works great except it only allows upper case post codes:
WA1 6EU is valid, wa1 6eu is not - But it should be!
How can I make the expression case-insensitive?
^([A-PR-UWYZ0-9][A-HK-Y0-9][AEHMNPRTVXY0-9]?[ABEHMNPRVWXY0-9]? {1,2}[0-9][ABD-HJLN-UW-Z]{2}|GIR 0AA)$
UPDATE: I am using DEVXPRESS asp.net controls, which have Regular Expression validation avaiable on certain controls. :
No case-insensitive key is allowed at the moment: http://www.devexpress.com/Support/Center/Question/Details/Q500302
I amended the expression as suggested below, but now upper case post codes are not valid - they were before:
^([A-PR-UWYZ0-9][a-pr-uwyz0-9][A-HK-Y0-9][a-hk-y0-9][AEHMNPRTVXY0-9][aehmnprtvxy0-9]?[ABEHMNPRVWXY0-9][abehmnprvwxy0-9]? {1,2}[0-9][ABD-HJLN-UW-Z][abd-hjln-uw-z]{2}|GIR 0AA)$
You can just enable the case insensitive flag i
Just wrap the expression as:
/yourexpression/i
See it on http://regex101.com/r/uX5rK9/1
Or you can use ToUpperCase() just before passing your string to the RegExp engine.
EDIT
This one should work if you can't set insensitive matching
^([A-Pa-pR-Ur-uWwYyZz0-9][A-Ha-hK-Yk-y0-9][AaEeHhMmNnPpRrTtVvXxYy0-9]?[AaBbEeHhMmNnPpRrVvWwXxYy0-9]? {1,2}[0-9][AaBbD-Hd-hJjLlN-Un-uW-Zw-z]{2}|(G|g)(I|i)(R|r) 0[Aa]{2})$
See it on http://regex101.com/r/yG4xD5/1
Option 1: change [A-PR-UWYZ0-9] into [A-PR-UWYZa-pr-uwyz0-9], and similarly for other [...]. This works regardless of engine.
Option 2: Turn on your regexp engine ignore case flag. You did not tag your post with a programming language, so we can't tell you how to do that, as it differs from language to language.

Google Spreadsheets Regex Case Insensitive (Regexreplace)

I'm trying to create a case insensitive regex query in Google Spreadsheets with the regexreplace function. Is that possible? I've tried the \i flag and got a #REF error saying the expression was invalid: =regexreplace("Test","t\i","") gives an error when I would hope to get "es" as the final result.
Is it possible? Is there a flag for case sensitivity in Google Spreadsheets?
Thanks in advance!
AFAIK, the only way to enable case-insensitive matching is JavaScript API in google docs.
Apparently, RE2 syntax does support the inline (?i) case-insensitive modifier:
=REGEXREPLACE("Test", "(?i)t", "")
An alternative that will work is using a Character class, adding both cases of the letter T..
=REGEXREPLACE("Test", "[Tt]", "")
I found this:
=REGEXREPLACE("Test","(?i)t","")
It returns what you want
es
google/re2
As Alexander Ivanov wrote,
Yes, I'm understand that this topic has resolved. But I found something fancy!
=REGEXREPLACE("Test","(?i)t","") It returns what you want
es
P.S.: Please, if somebody knows why it works then comment.
Google uses their own re2 regular expressions engine.
Using (?i) is allowing you to set the flags for case insensitive search
https://re2.googlecode.com/hg/doc/syntax.html
There is NO flag for case sensitivity
If you have a longer string and you want to make it case insensitive you cold try use a lowercase regular expression and make your test lowercase usign the function lower:
=REGEXREPLACE(LOWER(string), regex_in_lowercase, replacement)
in your specific case:
=REGEXREPLACE(LOWER("test"), "t", "")
The problem is that a capture expression with be in lowercase!
source: https://productforums.google.com/forum/#!topic/docs/7kNb9LGeIfM

Regular Expression to check for words

Here is my regular expression:
#"\<\\{+(TITLE|BODY|DATE|CALENDARID|CALENDARSUBSCRIPTIONGUID|CALENDARURL|TIME|CONTACTS|LOCATION|URLINFO)\\}+\\>"
It needs to check for words like {Title}. Have I made it correctly?
It looks ok to me. You might want to have the -i switch to make your expression case insensitive unless you only want to match "TITLE" and not "title" for example. Here is a pretty good regex checker: RegEx Pal. I noticed you didn't flag a language and the previous link was a javascript regex tester. Here are some more resources Regex Lib resources page