validate host names and IP addresses with single regex [closed] - javascript

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
Trying to combine two regexes from here to validate host names and IP addresses in a single statement.
I've got them working separately, but when combining using |, things get screwy, matching fragments of host names and ips, not whole valid patterns, which is not what I want. I need a single regex, using the host name and IP patterns below, to match either host or IP addresses.
I've provided scratch pads where I've tested each pattern; the last one is my attempt at combining them, with examples illustrating regex matching invalid fragments.
regex: Host Names
^((([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9]))$
regex: IP Address
^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3})$
regex: Host name and IPs (not working)
^((([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9]))|((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3})$

You had a precedence issue. When you wrote
^(regex1)|(regex2)$
It was interpreted as
(^(regex1))|((regex2)$)
So any line starting with a valid hostname or ending in an IP address matched ok. A solution is to do
^regex1$|^regex2$
Which comes out as:
^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3})$|^((([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9]))$

Related

Regex for email without spaces [duplicate]

This question already has an answer here:
Regular expression to not allow spaces anywhere in the email
(1 answer)
Closed 4 years ago.
("^[_a-zA-Z0-9!#$%&'*+-/=?^_`{|}~;]+(\\.[_a-zA-Z0-9!#$%&'*+-/=?^_`{|}~;]+)*#[a-zA-Z0-9-]+(\\.[a-zA-Z0-9-]+)*(\\.[a-zA-Z]{2,})$")
This is my regex for email field but I don't want to allow spaces in email. What I have to do please suggest me??
Unless you are trying to validate the email address as originating from a known domain, validating is practically impossible and bound to be frustrating for users with unusual addresses that your regex fails validation on.
For reference: https://davidcel.is/posts/stop-validating-email-addresses-with-regex/
Here is your current regex compared against a list of valid and invalid email addresses. As you can see, you failed to allow several, perfectly valid email addresses while still letting through around 30% of the invalid ones.

Can someone explain what this following regex means [duplicate]

This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 5 years ago.
/(.*?)((http:\/\/|https:\/\/)?[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,6}(\/[a-zA-Z0-9\-\.]+)*){1}(.*?)/g
I could only make some assumptions about the above regex. But most of it is cryptic to me.
(http:\/\/|https:\/\/) - It contains either http or https protocol.
[a-zA-Z]{2,6} - Contain any of the lower or uppercase characters between 2 and 6 times.
/g - Search for it recursively
But was not able to put all of the blocks together.
This looks like it's trying to match full URLs.
(http:\/\/|https:\/\/)?, as you mentioned, looks for an optional protocol prefix
(.*?) at the beginning and end match anything that may be before or after the URLs.
[a-zA-Z0-9\-\.]+ is likely attempting to match domain names and sub-domains (e.g. test.us.domain)
\.[a-zA-Z]{2,6} is matches top-level domains (e.g. .com, .us, .ninja)
(\/[a-zA-Z0-9\-\.]+)* is looking for paths (e.g. /about, /files/my-file001.txt)
{1} just one
This regex has it's faults for this purpose, for example some of the segments that allow . characters (e.g. [a-zA-Z0-9\-\.]+) would allow for them multiple times in a row (i.e. a...c...d) but generally speaking this should match on URLs provided the data around them doesn't look too much like URLs.

Best way to encode rest calls [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I have a rest call that sends addresses to the back-end. When an address contains a / e.g. c/o (corner of) I get a 400(bad request... i've tried to encodeURIComponent(query) but even though the address is encoded it still gives me the same error. i'm planning on replacing each occurance of / with $ in the rest call and then replacing the $ with a / again on the back-end. Is this the only way to go about this or is there a propper way of doing this?
The forward slash or / is a special char in URI encodings, it is used to separate arguments for the path to the file we want to access. So it obviously can not be used as we please. The encodeURIComponent function from Jquery will encode this character but it will be interpreted by your server as a keyword in most cases.
Your solutions are:
Either send using POST, which is the cleanest way to send text without having to care about its encoding.
Or replace the / with its html enity code which is: /, that way, you can send it via GET and it will still be understood by HTML as a / but no longer as a special character.
Hope it helps

RegEx for e-mail address outside code comment [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
Using RegEx, I am trying to match any e-mail address outside of a code comment.
For example, the string could be something like this:
/**
* Hello World
* For comment info e-mail us at test#email.com
*/
But we really want to match this#email.com
So in this case, I would only want to return 'this#email.com'. What RegEx formula could I use in this case?
Regular expressions are often seen as some magic language that will do anything we need with a text string if only we knew the incantation. It's not like that, and you shouldn't let your progammer-sense be subverted by the idea. Your core language should do the heavy lifting, leaving regex patterns to do the detailed work
It's unclear whether you need a Perl solution or a JavaScript one, or if you're equivocal. This is for Perl
How to "match any e-mail address outside of a code comment"?
Remove the comments
This is addressed in perlfaq6
Search for email addresses
You should use the
Regexp::Common module, together with
Regexp::Common::Email::Address.
But beware that this will match anything defined by the Internet Message Format standard, which may be rather more than you imagine an "email address" to be. It's a lot more than just bob#example.com
See also
Email::Address
if you need to process any email addresses that you find

PHP Get Values from a URL using GET[] [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I am trying to develop products filters for an online store I am working on. An example of what I mean is http://www.riverisland.com/men/just-arrived. I have managed to get a JavaScript to populate the URL when the sizes are clicked on but failed to get them remove value from URL when unchecked.
My main question here is this. Assuming I have my URL as:
http://127.0.0.1/shop/dresses/?s=1&s=2&s=3
How do I get my PHP to extract the values from the URL?
How do I format a SQL query to search the values gotten from the URL using any sample query?
An easier solution is this.
Format your URL like http://127.0.0.1/shop/dresses/?s=1,2,3 as suggested by #Andrey.Popov. Then do the below.
if(isset($_GET['s']) && !empty($_GET['s']))
{
$e = sanitizeFunction($_GET['s']);
$d=explode(',',$e);
}
$d now has all your $_GET['s'] values.
That's the easier way I have figured out and it works!
In order to benefit from $_GET and other superglobals you have to follow the rules explained at Variables From External Sources. Since you've chosen to have several parameters with the same name and they do not contain properly paired square brackets you're basically on your own. The manual steps you must reproduce include:
Extract the raw query string from $_SERVER['QUERY_STRING'], e.g.:
$query_string = filter_input(INPUT_SERVER, 'QUERY_STRING');
Parse out the string. As far as I know, there aren't built-in functions that do exactly this so I'd either google for a good third-party library or write a simple parser with regular expressions or good old explode().
Decode the URL-encoded values with urldecode()

Categories