<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Java &#8211; Learn Beginner</title>
	<atom:link href="https://learnbeginner.com/tag/java/feed/" rel="self" type="application/rss+xml" />
	<link>https://learnbeginner.com</link>
	<description>Start Your Tech Journey With Us &#124; Smart Learning, Great Beginning</description>
	<lastBuildDate>Tue, 06 Oct 2020 13:24:30 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.6.2</generator>

<image>
	<url>https://learnbeginner.com/wp-content/uploads/2021/02/favicon.png</url>
	<title>Java &#8211; Learn Beginner</title>
	<link>https://learnbeginner.com</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>HOW TO VALIDATE A DOMAIN NAME USING REGULAR EXPRESSION</title>
		<link>https://learnbeginner.com/how-to-validate-a-domain-name-using-regular-expression/</link>
					<comments>https://learnbeginner.com/how-to-validate-a-domain-name-using-regular-expression/#respond</comments>
		
		<dc:creator><![CDATA[Hiren Vaghasiya]]></dc:creator>
		<pubDate>Wed, 13 May 2020 16:46:10 +0000</pubDate>
				<category><![CDATA[Beginners Guide]]></category>
		<category><![CDATA[Dev Tips]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Dev]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Regular Expression]]></category>
		<category><![CDATA[Web Development]]></category>
		<guid isPermaLink="false">https://learnbeginner.com/?p=2660</guid>

					<description><![CDATA[In this technical and hacking era each and every developer used the validation using regular expression.]]></description>
										<content:encoded><![CDATA[
<p>Given string&nbsp;<strong>str</strong>, the task is to check whether the given string is a valid domain name or not by using&nbsp;Regular Expression.</p>



<p>The valid domain name must satisfy the following conditions:</p>



<ol class="wp-block-list"><li>The domain name should be a-z or A-Z or 0-9 and hyphen (-).</li><li>The domain name should be between 1 and 63 characters long.</li><li>The domain name should not start or end with a hyphen(-) (e.g. -learnbeginner.com or learnbeginner.com-).</li><li>The last TLD (Top level domain) must be at least two characters and a maximum of 6 characters.</li><li>The domain name can be a subdomain (e.g. hindi.learnbeginner.com).</li></ol>



<p><strong>Examples:</strong></p>


<p><strong>Input</strong>: str = “hindi.learnbeginner.com”<br><strong>Output</strong>: true<br><strong>Explanation</strong>:<br>The given string satisfies all the above-mentioned conditions. Therefore it is a valid domain name.</p>
<p><strong>Input</strong>: str = “-learnbeginner.com”<br><strong>Output</strong>: false<br><strong>Explanation</strong>:<br>The given string starts with a hyphen (-). Therefore it is not a valid domain name.</p>
<p><strong>Input</strong>: str = “learnbeginner.o”<br><strong>Output</strong>: false<br><strong>Explanation</strong>:<br>The given string has the last TLD of 1 character, the last TLD must be between 2 and 6 characters long. Therefore it is not a valid domain name.</p>
<p><strong>Input</strong>: str = “.com”<br><strong>Output</strong>: false<br><strong>Explanation</strong>:<br>The given string doesn’t start with a-z or A-Z or 0-9. Therefore it is not a valid domain name.</p>


<p><strong>Approach:</strong>&nbsp;The idea is to use&nbsp;Regular Expression&nbsp;to solve this problem. The following steps can be followed to compute the answer:</p>



<ol class="wp-block-list"><li>Get the String.</li><li>Create a regular expression to check valid domain name as mentioned below:<br><br>regex = “^((?!-)[A-Za-z0-9-]{1, 63}(?&lt;!-)\.)+[A-Za-z]{2, 6}$”<br><br>Where:<br><strong>^</strong>&nbsp;represents the starting of the string.<br><strong>(</strong>&nbsp;represents the starting of the group.<br><strong>(?!-)</strong>&nbsp;represents the string should not start with a hyphen (-).<br><strong>[A-Za-z0-9-]{1, 63}</strong>&nbsp;represents the domain name should be a-z or A-Z or 0-9 and hyphen (-) between 1 and 63 characters long.<br><strong>(?&lt;!-)</strong>&nbsp;represents the string should not end with a hyphen (-).<br><strong>\\.</strong>&nbsp;represents the string followed by a dot.<br><strong>)+</strong>&nbsp;represents the ending of the group, this group must appear at least 1 time, but allowed multiple times for subdomain.<br><strong>[A-Za-z]{2, 6}</strong>&nbsp;represents the TLD must be A-Z or a-z between 2 and 6 characters long.<br><strong>$</strong>&nbsp;represents the ending of the string.<br></li><li>Match the given string with the regular expression. In Java, this can be done by using&nbsp;Pattern.matcher().</li><li>Return true if the string matches with the given regular expression, else return false.</li></ol>



<pre class="wp-block-code"><code lang="java" class="language-java line-numbers">// Java program to validate domain name. 
// using regular expression. 
  
import java.util.regex.*; 
class GFG { 
  
    // Function to validate domain name. 
    public static boolean isValidDomain(String str) 
    { 
        // Regex to check valid domain name. 
        String regex = "^((?!-)[A-Za-z0-9-]"
                       + "{1,63}(?&lt;!-)\\.)"
                       + "+[A-Za-z]{2,6}"; 
  
        // Compile the ReGex 
        Pattern p = Pattern.compile(regex); 
  
        // If the string is empty 
        // return false 
        if (str == null) { 
            return false; 
        } 
  
        // Pattern class contains matcher() 
        // method to find the matching 
        // between the given string and 
        // regular expression. 
        Matcher m = p.matcher(str); 
  
        // Return if the string 
        // matched the ReGex 
        return m.matches(); 
    } 
  
    // Driver Code 
    public static void main(String args[]) 
    { 
        // Test Case 1: 
        String str1 = "learnbeginner.com"; 
        System.out.println(isValidDomain(str1)); 
  
        // Test Case 2: 
        String str2 = "hindi.learnbeginner.com"; 
        System.out.println(isValidDomain(str2)); 
  
        // Test Case 3: 
        String str3 = "-learnbeginner.com"; 
        System.out.println(isValidDomain(str3)); 
  
        // Test Case 4: 
        String str4 = "learnbeginner.o"; 
        System.out.println(isValidDomain(str4)); 
  
        // Test Case 5: 
        String str5 = ".com"; 
        System.out.println(isValidDomain(str5)); 
    } 
}</code></pre>



<p><br><strong>Output:</strong></p>


<pre>true<br />true<br />false<br />false<br />false</pre>


<h4 class="wp-block-heading">Thank you for reading, and let’s connect!</h4>



<p>Thank you for reading my blog, feel free to subscribe to my email newsletter, and ask questions and give your valuable suggestions.</p>


<script>(function() {
	window.mc4wp = window.mc4wp || {
		listeners: [],
		forms: {
			on: function(evt, cb) {
				window.mc4wp.listeners.push(
					{
						event   : evt,
						callback: cb
					}
				);
			}
		}
	}
})();
</script><!-- Mailchimp for WordPress v4.10.2 - https://wordpress.org/plugins/mailchimp-for-wp/ --><form id="mc4wp-form-1" class="mc4wp-form mc4wp-form-2830 mc4wp-form-theme mc4wp-form-theme-dark" method="post" data-id="2830" data-name="Main Newsletter" ><div class="mc4wp-form-fields"><p>
	<label> 
		<input type="email" name="EMAIL" placeholder="Your email address"  style="max-width: 100%;" required />
	</label>
	<input type="submit" value="Subscribe Now" style="width: 100%;" />
</p></div><label style="display: none !important;">Leave this field empty if you&#8217;re human: <input type="text" name="_mc4wp_honeypot" value="" tabindex="-1" autocomplete="off" /></label><input type="hidden" name="_mc4wp_timestamp" value="1742349656" /><input type="hidden" name="_mc4wp_form_id" value="2830" /><input type="hidden" name="_mc4wp_form_element_id" value="mc4wp-form-1" /><div class="mc4wp-response"></div></form><!-- / Mailchimp for WordPress Plugin -->]]></content:encoded>
					
					<wfw:commentRss>https://learnbeginner.com/how-to-validate-a-domain-name-using-regular-expression/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
