I have to validate below with the given condition
com.android.123foo
following conditions should be applied
Numbers shouldn't be allowed after the last dot (ex. "com.android.123foo")
NOTE: Numbers can be allowed middle or end of the string (ex. com.android.foo123 or com.adnroid.fo0123news)
special characters and hyphen should not be allowed over the entire string (ex com.android.123foo)
Kindly help.
Depending on which dot you are asking about the answer will change, but perhaps you are after something like this:
for after first dot
"com.iphones22tore.123foo".match /^.*\.[a-zA-Z.]*$/
=> nil
"com.iphonestore.foo".match /^.*\.[a-zA-Z.]*$/
=> #<MatchData "com.iphonestore.foo">
If you mean after the final dot then change the regex to:
.match /^.*\..*\.[a-zA-Z.]*$/
instead of creating a regex for a blacklist after the dot for what isnt allowed, this just simply defines what IS allowed after the dot.
Related
I am not very familiar with regex. I am trying to match routes in ruby. I have a situation where I have some /route/:id that can take the 'id' parameter. I would like to match the route to any string with parameters as long as there is no forward slash. So anything like /route/123 should match but anything like /route/subroute/123 should not since there is a forward slash after 'subroute'. This is my current regex pattern that matches the '/routes/' portion and allows any string to take place of the 'id' parameter: \A\/routes\/\z*. This works, but if a forward slash is present in the 'id' portion of the route, the match still succeeds. What can I do to allow any string as the 'id' as long as a forward slash is not present?
This ended up being the pattern that worked for my case:
^\/route(\/[A-Za-z0-9\-\_]*)?$
Since it is a route, I found it better to allow only valid url characters and I use the parentheses (...)? for cases of routes that do not take parameters at all so '/route', '/route/', and '/route/abc' will all work, but '/route/abc/' will not.
In Ruby, ^ marks the start of any line, not string, so instead of ^, you need \A. Same with $, to match a string end position, you need to use \z.
Also, to match a single path subpart (the string between two slashes here) you can use a negated character class [^\/]+ / [^\/]*. If you plan to restrict the chars in the subpart to alphanumeric, hyphen and underscore, you can replace [^\/] with [\w-].
So, you can also use
/\A\/route(?:\/[\w-]*)?\z/
Details:
\A - start of string
\/route - a literal /route string
(?:\/[\w-]*)? - an optional (due to the last ?) non-capturing group that matches an optional sequence of / and then zero or more alphanumeric, underscore (\w) or hyphen chars
\z - end of string.
See the Rubular demo (here, ^ and $ are used for demo purposes only since the input is a single multiline text).
I have a field "floors" and I want it to accept only numbers, commas and white spaces.
I'm using a validates_format_of :floors, :with => /[0-9\,\s]+/ right now, but it works bad because it accepts a string like "1, 2, abc".
Please help me to find my mistake.
Your regex matches 1, 2, inside 1, 2, abc, it is a partial match. To disallow partial matches, use start-of-string and end-of-string anchors.
In Ruby, to match the start of the string you need to use \A anchor. The end-of-string anchor is \z. Thus, use
/\A[0-9,\s]+\z/
See regex demo
Also note that , is not a special regex metacharacter and does not need escaping.
If you need to start with a number, you can use
/\A\d[\d,\s]*\z/
Here, \d will require a digit to appear in the beginning and then it can be followed with digits, whitespace and commas, zero or more occurrences. Another way of restricting the generic character class is using a lookahead: \A(?=\d)[\d,\s]+\z.
Going further, you can match numbers like 1,300,567.567 or 1 300 567.567 with
/\A\d{1,3}(?:[,\s]\d{3})*(?:\.\d+)?\z/
See another demo
Is there a reserved query-string character that is conventionally used to ignore sections of the string? Something like this:
?foo=bar&baz=quux*some character*&sky=blue
Where adding some character results in this query string with the end ignored:
?foo=bar&baz=quux
I know that it's usually up to the programs parsing the query string to decide how anything is handled, but I wanted to know if anything like this exists in the same way that a + is meant to denote a space.
The fragment identifier, indicated by #, has such an effect:
The query component is indicated by the first question
mark ("?") character and terminated by a number sign ("#") character
or by the end of the URI.
I'm attempting to validate a string of user input that will be used as a subdomain. The rules are as follows:
Between 1 and 63 characters in length (I take 63 from the number of characters Google Chrome appears to allow in a subdomain, not sure if it's actually a server directive. If you have better advice on valid max length, I'm interested in hearing it)
May contain a-zA-Z0-9, hyphen, underscore
May not begin or end with a hyphen or underscore
EDIT: From input below, I've added the following:
4. Should not contain consecutive hyphens or underscores.
Examples:
a => valid
0 => valid
- => not valid
_ => not valid
a- => not valid
-a => not valid
a_ => not valid
_a => not valid
aa => valid
aaa => valid
a-a-a => valid
0-a => valid
a&a => not valid
a-_0 => not valid
a--a => not valid
aaa- => not valid
My issue is I'm not sure how to specify with a RegEx that the string is allowed to be only one character, while also specifying that it may not begin or end with a hyphen or underscore.
Thanks!
You can't can have underscores in proper subdomains, but do you need them? After trimming your input, do a simple string length check, then test with this:
/^[a-z\d]+(-[a-z\d]+)*$/i
With the above, you won't get consecutive - characters, e.g. a-bbb-ccc passes and a--d fails.
/^[a-z\d]+([-_][a-z\d]+)*$/i
Will allow non-consecutive underscores as well.
Update: you'll find that, in practice, underscores are disallowed and all subdomains must start with a letter. The solution above does not allow internationalised subdomains (punycode). You're better of using this
/\A([a-z][a-z\d]*(-[a-z\d]+)*|xn--[\-a-z\d]+)\z/i
I'm not familiar with Ruby regex syntax, but I'll assume it's like, say, Perl. Sounds like you want:
/^(?![-_])[-a-z\d_]{1,63}(?<![-_])$/i
Or if Ruby doesn't use the i flag, just replace [-a-z\d_] with [-a-zA-Z\d_].
The reason I'm using [-a-zA-Z\d_] instead of the shorter [-\w] is that, while nearly equivalent, \w will allow special characters such as รค rather than just ASCII-type characters. That behavior can be optionally turned off in most languages, or you can allow it if you like.
Some more information on character classes, quantifiers, and lookarounds
/^([a-z0-9][a-z0-9\-\_]{0,61}[a-z0-9]|[a-z0-9])$/i
I've took it as a challenge to create a regex that should match only strings with non-repeating hyphens or underscores and also check the proper length for you:
/^([a-z0-9]([_\-](?![_\-])|[a-z0-9]){0,61}[a-z0-9]|[a-z0-9])$/i
The middle part uses a lookaround to verify that.
^[a-zA-Z]([-a-zA-Z\d]*[a-zA-Z\d])?$
This simply enforces the standard in an efficient way without backtracking. It does not check the length, but Regex is inefficient at things like that. Just check the string length (1 to 64 chars).
/[^\W\_](.+?)[^\W\_]$/i should work for ya (try our http://rubular.com/ to test out regular expressions)
EDIT: actually, this doesn't check single/double letter/numbers. try /([^\W\_](.+?)[^\W\_])|([a-z0-9]{1,2})/i instead, and tinker with it in rubular until you get exactly what ya want (if this doesn't take care of it already).
I am trying to find a regex to limit what a person can use for a username on my site. I don't need to have it check to see how many characters there are in it, as another validation does this. Basically all I need to make it do is make sure that it allows: letters (capital and lowercase) numbers, dashes and underscores.
I came across this: /^[-a-z]+$/i
But it doesn't seem to allow numbers.
What am I missing?
The regex you're looking for is
/\A[a-z0-9\-_]+\z/i
Meaning one or more characters of range a-z, range 0-9, - (needs to be escaped with a backslash) and _, case insensitive (the i qualifier)
Use
/\A[\w-]+\z$/
\w is shorthand for letters, digits and underscore.
\A matches at the start of the string, \z matches at the end of the string. These tokens are called anchors, and Ruby is a bit special with regard to them: Most regex engines use ^ and $ as start/end-of-string anchors by default, whereas in Ruby they can also match at the start/end of lines (which matters if you're working with multiline strings). Therefore, it's safer (as #JustMichael pointed out) to use \A and \z because there is no such ambiguity.
Your regular expression contains a character class [-a-z] that allows the characters - (dash) and a through z. In order to expand the range of characters allowed by this character class, you will need to add more characters within the [].
Please see Character Classes or Character Sets for further information and examples.