Check user input is valid CSS width value - regex

I'm allowing a user to type in a width value that I need to ensure is a valid CSS width value.
Using regex, how would I get two matches from the user input where $matches[0] = a number value AND $matches[1] = either px or %?
Thanks!

(\d*)(px|%)?
First captured item will be the number, the second captured item will be px or %.

Related

Regex to detect string is x.x.x where x is a digit from 1-3 digits

I have values 1000+ rows with variable values entered as below
5.99
5.188.0
v5.33
v.440.0
I am looking in Gsheet another column to perform following operations:
Remove the 'v' character from the values
if there is 2nd '.' missing as so string can become 5.88 --> 5.88.0
Can help please in the regex and replace logic as tried this but new to regex making. Thanks for the help given
=regexmatch(<cellvalue>,"^[0-9]{1}\.[0-9]{1,3}\.[0-9]{1,3}$")
I have done till finding the value as 5.88.0 returns TRUE and 5.99 returns false, need to now append ".0" so 5.99 --> 5.99.0 and remove 'v' if found.
You can use a combination of functions, it may not be pretty, but it does the work
Replace any instance of v with an empty string using substitute, by making the content of the cell upper case, if we don't put UPPER(CELL) we could exclude any upper case V or lower case v(it will depend which function you use)
SUBSTITUTE(text_to_search, search_for, replace_with, [occurrence_number])
=SUBSTITUTE(UPPER(A1),"V","")
Look for cell missing the last block .xxx, you need to update a bit your regex to specified that the last group it's not present
^([0-9]{1}\.[0-9]{1,3} ( \.[0-9]{1,3}){0} )$
Using REGEXMATCH and IF we can then CONCATENATE the last group as .0
REGEXMATCH(text, regular_expression)
CONCATENATE(string1, [string2, ...])
=IF(REGEXMATCH(substitute(upper(A2),"V",""),"^([0-9]{1}\.[0-9]{1,3}(\.[0-9]{1,3}){0})$"),concatenate(A2,".0"), A2)
The last A2 will be replace with something similar than what we have until now, but before that we need to make small change in the regex, we want to look for the groups you specified were the last group it's present, that's your orignal regex, if it meets the regex it will put it in the cell, otherwise it will put INVALID, you can change that to anything you want it to be
^([0-9]{1}.[0-9]{1,3}.[0-9]{1,3})$
This it's the piece we are putting instead of the last A2
IF(REGEXMATCH(substitute(upper(A2),"V",""),"^([0-9]{1}\.[0-9]{1,3}\.[0-9]{1,3})$"),substitute(upper(A2),"V",""),"INVALID")
With this the final code to put in your cell will be:
=IF(REGEXMATCH(substitute(upper(A2),"V",""),"^([0-9]{1}\.[0-9]{1,3}(\.[0-9]{1,3}){0})$"),concatenate(SUBSTITUTE(UPPER(A2),"V",""),".0"),IF(REGEXMATCH(substitute(upper(A2),"V",""),"^([0-9]{1}\.[0-9]{1,3}\.[0-9]{1,3})$"),substitute(upper(A2),"V",""),"INVALID"))

Autohotkey extract text using regex

I am just now learning regex using autohotkey but can't figure out how to extract specific string and save to a variable?
Line of text I am searching:
T NW CO NORWALK HUB NW 201-DS3-WLFRCTAICM5-NRWLCT02K16 [DS3 LEC] -1 -1 PSTN
I am trying to save, NW 201-DS3-WLFRCTAICM5-NRWLCT02K16 [DS3 LEC] ONLY.
Here is my regex code:
NW\D\d.DS3.]
But how do I store that as a variable in autohotkey?
I have tried RegexMatch but that only shows the position. I am doing something wrong.
You may provide the third argument that will hold the match array:
RegExMatch(str,"NW\D\d.*DS3.*\]",matches)
Then, matches[0] will contain the match.
If you use capturing groups inside the pattern, you will be able to access their values by using further indices. If you use "NW\D(\d.*DS3.*)\]" against "NW 5xxx DS3 yyy], you will have the whole string inside matches[0] and matches[1] will hold 5xxx DS3 yyy.
See AHK RegExMatch docs:
FoundPos := RegExMatch(Haystack, NeedleRegEx [, UnquotedOutputVar = "", StartingPosition = 1])
UnquotedOutputVar
Mode 1 (default): OutputVar is the unquoted name of a variable in which to store the part of Haystack that matched the entire pattern. If the pattern is not found (that is, if the function returns 0), this variable and all array elements below are made blank.
If any capturing subpatterns are present inside NeedleRegEx, their matches are stored in a pseudo-array whose base name is OutputVar. For example, if the variable's name is Match, the substring that matches the first subpattern would be stored in Match1, the second would be stored in Match2, and so on. The exception to this is named subpatterns: they are stored by name instead of number. For example, the substring that matches the named subpattern "(?P<Year>\d{4})" would be stored in MatchYear. If a particular subpattern does not match anything (or if the function returns zero), the corresponding variable is made blank.
; If you want to delete ALL ....
Only(ByRef C)
{
/*
RegExReplace
https://autohotkey.com/docs/commands/RegExReplace.htm
*/
; NW 201-DS3-WLFRCTAICM5-NRWLCT02K16 [DS3 LEC]
C:=RegExReplace(C, "NW\s[\w-]+\s\[[\w\s]+\]","",ReplacementCount,-1)
if (ReplacementCount = 0)
return C
else
return Only(C)
} ; Only(ByRef C)
string:="Line of text I am searching: T NW CO NORWALK HUB NW 201-DS3-WLFRCTAICM5-NRWLCT02K16 [DS3 LEC] -1 -1 PSTN"
Result:=Only(string)
MsgBox, % Result
MsgBox, % Only(string)

Regular expression for field and value

I've text as:
field = a value = 1345 field = b value = 3453
I need regular expression so that I can extract data as:
field value
a 1345
b 3453
What can be the perfect regex for it?
The expression should be:
field\s=\s([^\s]+)\svalue\s=\s([^\s]+)
Regex demo.
There are two capturing groups: one for field and one for value. Also note that I have specified any character other than space as a value for field or value - you could make it stricter.
Try this:
field\s*=\s*([^\s]+)\s*value\s*=\s*([^\s]+)
demo
it fill up more than one spaces case like field = a value = 1345 field = b value = 3453

can any one tell me regular expression for this? UPDATED

I tried many syntax in vistal studio, and in this site, but nothing helped.
The expression would be _ct_(anyDigitHere)_
like
adkasjflasdjfsdf asdfkl sjfdsf _ct150_ asdfasd // so it would match this _ct150
any thing here doens't matter Random stuff..afd a&r9qwr89 ((
_ct415487_ anything here doesn't matter // this will match _ct415487_
basically any _ctAndAnyNumberHere_ (underscore at start and end)
A couple I tried ^*(_ct)(:z)(+_)+*$, ^*(_ct[0-9]+_)+*$. But none helps!
EDIT
Thanks for the reply(ies). That did work, but what I now the problem is replace those matched elements with a hidden field value.. say..
if the value in hidden field is of 1 digit, (any value from 0-9), I have to take last digit from that matched expression and replace it with the value in hidden field.
if the value in hidden field is of 2 digit, (any value from 0-99), I have to take last two digits from that matched expression and replace it with the value in hidden field.
so, basically..
if the value in hidden field is of n digit, I have to take last n digits from that matched expression and replace it with the value in hidden field.
How do I do that?
I don't know what language of visual studio you're talking about, but this should work:
_ct\d+_
or this:
_ct([0-9]+)_
EDIT:
Regex rg = new Regex("_ct([0-9]+)_");
string text = "any thing here doens't matter Random stuff..afd a&r9qwr89 ((_ct415487_ anything here doesn't matter";
var match = rg.Match(text).Groups[1].Value;
int sizeHiddenField = HiddenField1.Value.Length;
var newString = text.Replace(match, match.Substring(0, match.Length - sizeHiddenField) + HiddenField1.Value);
/_ct\d*_/
This is the regular expression syntax for your given problem. Try this

Regular Expression With Mask

I have a regular expression for phone numbers as follows:
^[01]?[- .]?(\([2-9]\d{2}\)|[2-9]\d{2})[- .]?\d{3}[- .]?\d{4}$
I have a mask on the phone number textbox in the following format: (___)___-____
How can I modify the regular expression so that it accommodates the mask?
The expression for the (placeholder) mask is
^\(_{3}\)_{3}-_{4}$
The expression for a valid phone number is
^\(\d{3}\)\d{3}-\d{4}$
The mask uses _ in place of digits, so you'll need to use [\d_] as your character class to match as the user is typing.
^\([\d_]{3}\)[\d_]{3}-[\d_]{4}$
Obviously, if the user switches fields, you'll want to return an error if your phone field as any remaining _ in it. phone.match(/_/) == null should do the trick here :)
Your question is a little unclear; if you want a regular expression that matches that mask, it's:
^\([0-9]{3}\)[0-9]{3}-[0-9]{4}$
ValidationExpression="\([2-9]\d{2}\)\d{3}-\d{4}$|^\(_{3}\)\ _{3}-_{4}$"
This will validate the mask and (234)432-4322 but won't allow the user to enter (434)88_-____
this expression will work for you
^[( ][0-9][1-9][0-9][) ]?[0-9]{3}[- ]?[0-9]{4}$
this expression defines that except second digit of phone number every digit will consider
{0}