Regular expression to restrict length - jquery-ui

I have a Regex for my Jquery
[RegularExpressionAttribute(#"/^[-\w\s]+$/")] which works for alphanumeric and space. But I want it to restrict its length to 147 characters.
Please help!

Use [RegularExpressionAttribute(#"/^[-\w\s]{0,147}$/")]

If you are talking about jquery or javascript, then you have to use regular expression as below.
var regex = /^[-\w\s]{0,147}$/;
alert(regex.test(anyString))
It alerts true if "anyString" matches the given regular expression, and otherwise false.

Related

Rails strip all except numbers commas and decimal points

Hi I've been struggling with this for the last hour and am no closer. How exactly do I strip everything except numbers, commas and decimal points from a rails string? The closest I have so far is:-
rate = rate.gsub!(/[^0-9]/i, '')
This strips everything but the numbers. When I try add commas to the expression, everything is getting stripped. I got the aboves from somewhere else and as far as I can gather:
^ = not
Everything to the left of the comma gets replaced by what's in the '' on the right
No idea what the /i does
I'm very new to gsub. Does anyone know of a good tutorial on building expressions?
Thanks
Try:
rate = rate.gsub(/[^0-9,\.]/, '')
Basically, you know the ^ means not when inside the character class brackets [] which you are using, and then you can just add the comma to the list. The decimal needs to be escaped with a backslash because in regular expressions they are a special character that means "match anything".
Also, be aware of whether you are using gsub or gsub!
gsub! has the bang, so it edits the instance of the string you're passing in, rather than returning another one.
So if using gsub! it would be:
rate.gsub!(/[^0-9,\.]/, '')
And rate would be altered.
If you do not want to alter the original variable, then you can use the version without the bang (and assign it to a different var):
cleaned_rate = rate.gsub!(/[^0-9,\.]/, '')
I'd just google for tutorials. I haven't used one. Regexes are a LOT of time and trial and error (and table-flipping).
This is a cool tool to use with a mini cheat-sheet on it for ruby that allows you to quickly edit and test your expression:
http://rubular.com/
You can just add the comma and period in the square-bracketed expression:
rate.gsub(/[^0-9,.]/, '')
You don't need the i for case-insensitivity for numbers and symbols.
There's lots of info on regular expressions, regex, etc. Maybe search for those instead of gsub.
You can use this:
rate = rate.gsub!(/[^0-9\.\,]/g,'')
Also check this out to learn more about regular expressions:
http://www.regexr.com/

How to use FParsec to parse identifiers with different start and end characters

I'm having difficulty working out the best way to parse identifiers that have different characters at the start and end. For example, let's say that the start characters of our identifiers may be upper and lowercase only, while the middle of an identifier may also include digits and colons. The end of an identifier may not be a colon, but may be an apostrophe.
So the following are all legal identifiers:
f, f0, f:', f000:sdfsd:asdf
But the following are not:
0, hello:, he'llo
I can't see how best to handle the backtracking: a colon is fine in the middle, but we need some lookahead to determine whether we are at the end of the identifier.
EDIT:
Thanks for the suggestions. Using a regex is a pragmatic approach, but I find it slightly disappointing that there doesn't seem to be clean/obvious way of doing this otherwise.
I also think you should use regex, however I came up with a different pattern:
let pattern = regex #"^([a-zA-Z]+[a-zA-Z0-9:]*[a-zA-Z']?)$"
which will hold all of your wanted Matches in the first group. You can use an online RegExp tool to validate your matches/grouping.
You can handle this with a regex parser
let ident = regex #"[A-Za-z][A-Za-z0-9\:]*[A-Za-z0-9\']"
http://www.quanttec.com/fparsec/reference/charparsers.html

Regular expression which allows alphabets, dashes(-) and dot(.)

I'm trying to write a regex which allows alphabets, dot . and dashes - (for validation)
But couldn't find a valid regex which would do so, please help!
Thanks in advance
I think this will work for you
^[a-zA-Z-.]*$
any lowercase letter of the alphabet, any uppercase letter of the alphabet, dash as a group in any combination appearing 1 or many times
This character class should work for you:
[a-zA-Z.-]
Must Read: http://regular-expressions.info
Use this regex ([A-Za-z.-]) and test here http://www.rubular.com/r/H3Axvol13b
(?i)[a-z.-]
(?i) Will find any character no matter what case

URL Escape in Uppercase

I have a requirement to escape a string with url information but also some special characters such as '<'.
Using cl_http_utility=>escape_url this translates to '%3c'. However due to our backend webserver, it is unable to recognize this as special character and takes the value literally. What it does recognize as special character is '%3C' (C is upper case). Also if one checks http://www.w3schools.com/tags/ref_urlencode.asp it shows the value with all caps as the proper encoding.
I guess my question is is there an alternative to cl_http_utility=>escape_url that does essentially the same thing except outputs the value in upper case?
Thanks.
Use the string function.
l_escaped = escape( val = l_unescaped
format = cl_abap_format=>e_url ).
Other possible formats are e_url_full, e_uri, e_uri_full, and a bunch of xml/json stuff too. The string function escape is documented pretty well, demo programs and all.

Bug in my regular expression

I'm trying to look at a string and reject anything that has seq= or app= in the string. Where it gets tricky is I need elements with q=something or p=something.
The seq= part of the string is always preceded an & and app= is always preceded by a ?
I have absolutely no idea where to start. I've been using http://www.rubular.com/ to try and figure it out but to no avail.
Any help would be hugely appreciated.
Based on your question, I believe you could just reject any strings that match the following expression:
[\?&](?:seq|app)=
This will match any string that contains a ? or & followed by either app= or seq=. The ?: inside the parentheses just tells the regular expression not to bother to capture matching groups as sub-matches. They're not really necessary, but what the heck.
Here's a Rubular link with some samples.

Resources