📌  相关文章
📜  如何使用正则表达式验证MAC地址

📅  最后修改于: 2021-04-29 15:41:44             🧑  作者: Mango

给定字符串str ,任务是使用正则表达式检查给定字符串是否为有效的MAC地址。

例子:

方法:想法是使用正则表达式解决此问题。可以按照以下步骤计算答案。

  • 获取字符串。
  • 创建一个正则表达式以检查有效的MAC地址,如下所述:
  • 在哪里:
    • ^表示字符串的开头。
    • ([0-9A-Fa-f] {2} [:-]){5}代表由十六进制数字组成的五组,用连字符(-)或冒号(:)分隔
    • ([0-9A-Fa-f] {2})代表一组两个十六进制数字。
    • |代表or。
    • (代表小组的开始。
    • [0-9a-fA-F] {4} \\。代表由点(。)分隔的四个十六进制数字的第一部分。
    • [0-9a-fA-F] {4} \\。代表由点(。)分隔的四个十六进制数字的第二部分。
    • [0-9a-fA-F] {4}代表四个十六进制数字的第三部分。
    • )代表群组的结尾。
    • $表示字符串的结尾。
  • 将给定的字符串与正则表达式匹配。在Java,这可以通过使用Pattern.matcher()来完成。
  • 如果字符串与给定的正则表达式匹配,则返回true;否则返回false。

下面是上述方法的实现:

Java
// Java program to validate
// MAC address using
// regular expression
 
import java.util.regex.*;
class GFG {
 
    // Function to validate
    // MAC address
    // using regular expression
    public static boolean isValidMACAddress(String str)
    {
        // Regex to check valid
        // MAC address
        String regex = "^([0-9A-Fa-f]{2}[:-])"
                       + "{5}([0-9A-Fa-f]{2})|"
                       + "([0-9a-fA-F]{4}\\."
                       + "[0-9a-fA-F]{4}\\."
                       + "[0-9a-fA-F]{4})$";
 
        // Compile the ReGex
        Pattern p = Pattern.compile(regex);
 
        // If the string is empty
        // return false
        if (str == null)
        {
            return false;
        }
 
        // Find match between given string
        // and regular expression
        // uSing Pattern.matcher()
 
        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 = "01-23-45-67-89-AB";
        System.out.println(isValidMACAddress(str1));
 
        // Test Case 2:
        String str2 = "01:23:45:67:89:AB";
        System.out.println(isValidMACAddress(str2));
 
        // Test Case 3:
        String str3 = "0123.4567.89AB";
        System.out.println(isValidMACAddress(str3));
 
        // Test Case 4:
        String str4 = "01-23-45-67-89-AH";
        System.out.println(isValidMACAddress(str4));
 
        // Test Case 5:
        String str5 = "01-23-45-67-AH";
        System.out.println(isValidMACAddress(str5));
    }
}


Python3
# Python3 program to validate
# MAC address using
# using regular expression
import re
 
# Function to validate MAC address.
 
 
def isValidMACAddress(str):
 
    # Regex to check valid
    # MAC address
    regex = ("^([0-9A-Fa-f]{2}[:-])" +
             "{5}([0-9A-Fa-f]{2})|" +
             "([0-9a-fA-F]{4}\\." +
             "[0-9a-fA-F]{4}\\." +
             "[0-9a-fA-F]{4})$")
 
    # Compile the ReGex
    p = re.compile(regex)
 
    # If the string is empty
    # return false
    if (str == None):
        return False
 
    # Return if the string
    # matched the ReGex
    if(re.search(p, str)):
        return True
    else:
        return False
 
# Driver code
 
 
# Test Case 1:
str1 = "01-23-45-67-89-AB"
print(isValidMACAddress(str1))
 
# Test Case 2:
str2 = "01:23:45:67:89:AB"
print(isValidMACAddress(str2))
 
# Test Case 3:
str3 = "0123.4567.89AB"
print(isValidMACAddress(str3))
 
# Test Case 4:
str4 = "01-23-45-67-89-AH"
print(isValidMACAddress(str4))
 
# Test Case 5:
str5 = "01-23-45-67-AH"
print(isValidMACAddress(str5))
 
# This code is contributed by avanitrachhadiya2155


C++
// C++ program to validate the
// MAC address
// using Regular Expression
#include 
#include 
using namespace std;
 
// Function to validate the MAC address
bool isValidMACAddress(string str)
{
 
    // Regex to check valid MAC address
    const regex pattern(
    "^([0-9A-Fa-f]{2}[:-]){5}"
      "([0-9A-Fa-f]{2})|([0-9a-"
    "fA-F]{4}\\.[0-9a-fA-F]"
      "{4}\\.[0-9a-fA-F]{4})$");
 
    // If the MAC address
    // is empty return false
    if (str.empty())
    {
        return false;
    }
 
    // Return true if the MAC address
    // matched the ReGex
    if (regex_match(str, pattern))
    {
        return true;
    }
    else
    {
        return false;
    }
}
 
// Driver Code
int main()
{
    // Test Case 1:
    string str1 = "01-23-45-67-89-AB";
    cout << isValidMACAddress(str1) << endl;
 
    // Test Case 2:
    string str2 = "01:23:45:67:89:AB";
    cout << isValidMACAddress(str2) << endl;
 
    // Test Case 3:
    string str3 = "0123.4567.89AB";
    cout << isValidMACAddress(str3) << endl;
 
    // Test Case 4:
    string str4 = "01-23-45-67-89-AH";
    cout << isValidMACAddress(str4) << endl;
 
    // Test Case 5:
    string str5 = "01-23-45-67-AH";
    cout << isValidMACAddress(str5) << endl;
 
    return 0;
}
 
// This code is contributed by yuvraj_chandra


输出
true
true
true
false
false