Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/379.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 正则表达式在不同的语言环境下工作_Java_Regex - Fatal编程技术网

Java 正则表达式在不同的语言环境下工作

Java 正则表达式在不同的语言环境下工作,java,regex,Java,Regex,我使用java中的ipconfig/all命令和正则表达式来查找MAC地址 我正在ipconfig/all命令的输出中搜索Physical Address 但问题是我希望正则表达式在不同的语言环境下工作,即它可以找到任何语言环境的物理地址 提前感谢。选项1: 您可以使用这样的regexp(英语、法语、西班牙语): 稍后检查您使用的语言环境,并相应地更新正则表达式 选项2: 直接使用Java(JDK1.6)获取MAC地址 import java.net.InetAddress; import ja

我使用java中的
ipconfig/all
命令和正则表达式来查找MAC地址

我正在
ipconfig/all
命令的输出中搜索
Physical Address

但问题是我希望正则表达式在不同的语言环境下工作,即它可以找到任何语言环境的物理地址


提前感谢。

选项1:

您可以使用这样的regexp(英语、法语、西班牙语):

稍后检查您使用的语言环境,并相应地更新正则表达式

选项2:

直接使用Java(JDK1.6)获取MAC地址

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;

public class MacAddress {

public static void main(String[] args) {
    try {
        //InetAddress address = InetAddress.getLocalHost();
        InetAddress address = InetAddress.getByName("192.168.0.158");

        /*
         * Get NetworkInterface for the current host and then read the
         * hardware address.
         */
        NetworkInterface ni = NetworkInterface.getByInetAddress(address);
        if (ni != null) {
            byte[] mac = ni.getHardwareAddress();
            if (mac != null) {
                /*
                 * Extract each array of mac address and convert it to hexa with the
                 * following format 08-00-27-DC-4A-9E.
                 */
                for (int i = 0; i < mac.length; i++) {
                    System.out.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : "");
                }
            } else {
                System.out.println("Address doesn't exist or is not accessible.");
            }
        } else {
            System.out.println("Network Interface for the specified address is not found.");
        }
    } catch (UnknownHostException e) {
        e.printStackTrace();
    } catch (SocketException e) {
        e.printStackTrace();
    }
}
导入java.net.InetAddress;
导入java.net.NetworkInterface;
导入java.net.SocketException;
导入java.net.UnknownHostException;
公共类MacAddress{
公共静态void main(字符串[]args){
试一试{
//InetAddress=InetAddress.getLocalHost();
InetAddress=InetAddress.getByName(“192.168.0.158”);
/*
*获取当前主机的NetworkInterface,然后读取
*硬件地址。
*/
NetworkInterface ni=NetworkInterface.getByInetAddress(地址);
如果(ni!=null){
字节[]mac=ni.getHardwareAddress();
如果(mac!=null){
/*
*提取每个mac地址数组,并使用
*以下格式为08-00-27-DC-4A-9E。
*/
for(int i=0;i

}

选项1:

您可以使用这样的regexp(英语、法语、西班牙语):

稍后检查您使用的语言环境,并相应地更新正则表达式

选项2:

直接使用Java(JDK1.6)获取MAC地址

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;

public class MacAddress {

public static void main(String[] args) {
    try {
        //InetAddress address = InetAddress.getLocalHost();
        InetAddress address = InetAddress.getByName("192.168.0.158");

        /*
         * Get NetworkInterface for the current host and then read the
         * hardware address.
         */
        NetworkInterface ni = NetworkInterface.getByInetAddress(address);
        if (ni != null) {
            byte[] mac = ni.getHardwareAddress();
            if (mac != null) {
                /*
                 * Extract each array of mac address and convert it to hexa with the
                 * following format 08-00-27-DC-4A-9E.
                 */
                for (int i = 0; i < mac.length; i++) {
                    System.out.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : "");
                }
            } else {
                System.out.println("Address doesn't exist or is not accessible.");
            }
        } else {
            System.out.println("Network Interface for the specified address is not found.");
        }
    } catch (UnknownHostException e) {
        e.printStackTrace();
    } catch (SocketException e) {
        e.printStackTrace();
    }
}
导入java.net.InetAddress;
导入java.net.NetworkInterface;
导入java.net.SocketException;
导入java.net.UnknownHostException;
公共类MacAddress{
公共静态void main(字符串[]args){
试一试{
//InetAddress=InetAddress.getLocalHost();
InetAddress=InetAddress.getByName(“192.168.0.158”);
/*
*获取当前主机的NetworkInterface,然后读取
*硬件地址。
*/
NetworkInterface ni=NetworkInterface.getByInetAddress(地址);
如果(ni!=null){
字节[]mac=ni.getHardwareAddress();
如果(mac!=null){
/*
*提取每个mac地址数组,并使用
*以下格式为08-00-27-DC-4A-9E。
*/
for(int i=0;i
}

您可以只编写一个正则表达式来查找MAC地址本身,而不必搜索“物理地址”或该地址的任何其他本地化版本(这将要求您在每次需要支持新语言时添加本地化版本)

因为我们知道一个典型的MAC地址由六个组组成,每个组由两个十六进制数字组成,由冒号、句点或破折号分隔,下面的正则表达式可以做到这一点:

([a-fA-F0-9]{2}[:\-\.]){5}[a-fA-F0-9]{2}
说明: (两个十六进制数字后跟一个冒号:,重复5次)(最后两个十六进制数字)

而不是搜索“物理地址”或该地址的任何其他本地化版本(这将要求您在每次需要支持新语言时添加本地化版本),您只需编写一个正则表达式即可找到MAC地址本身

因为我们知道一个典型的MAC地址由六个组组成,每个组由两个十六进制数字组成,由冒号、句点或破折号分隔,下面的正则表达式可以做到这一点:

([a-fA-F0-9]{2}[:\-\.]){5}[a-fA-F0-9]{2}
说明:
(两个十六进制数字后跟冒号:,重复5次)(最后两个十六进制数字)

以下是我运行
ipconfig/all时得到的结果:

Adaptér sítě Ethernet Připojení k místní síti:
        Přípona DNS podle připojení . . . : example.com
        Popis . . . . . . . . . . . . . . : AMD PCNET Family PCI Ethernet Adapter
        Fyzická Adresa. . . . . . . . . . : DE-AD-BE-EF-CA-FE
        Protokol DHCP povolen . . . . . . : Ano
        Automatická konfigurace povolena  : Ano
        Adresa IP . . . . . . . . . . . . : 192.168.0.158
        Maska podsítě . . . . . . . . . . : 255.255.255.0
        Výchozí brána . . . . . . . . . . : 192.168.0.1
        Server DHCP . . . . . . . . . . . : 192.168.0.1
        Servery DNS . . . . . . . . . . . : 192.168.0.1
        Primární server WINS. . . . . . . : 192.168.0.1
        Zapůjčeno . . . . . . . . . . . . : 9. září 2011 16:05:32
        Zápůjčka vyprší . . . . . . . . . : 9. září 2011 20:05:32
如您所见,查找字符串“物理地址”有些徒劳,因为没有物理地址。但是,请注意,Windows有自己的MAC地址格式——每两个十六进制数字之间用连字符分隔(字母部分大写)。因此,寻找正则表达式:

([0-9A-F]{2}-){5}[0-9A-F]{2}
会给你你要找的MAC地址


警告:许多计算机有多个网络接口(有线和wifi、各种VPN等),因此在输出中可能会发现多个Mac。

以下是我运行
ipconfig/all时得到的结果:

Adaptér sítě Ethernet Připojení k místní síti:
        Přípona DNS podle připojení . . . : example.com
        Popis . . . . . . . . . . . . . . : AMD PCNET Family PCI Ethernet Adapter
        Fyzická Adresa. . . . . . . . . . : DE-AD-BE-EF-CA-FE
        Protokol DHCP povolen . . . . . . : Ano
        Automatická konfigurace povolena  : Ano
        Adresa IP . . . . . . . . . . . . : 192.168.0.158
        Maska podsítě . . . . . . . . . . : 255.255.255.0
        Výchozí brána . . . . . . . . . . : 192.168.0.1
        Server DHCP . . . . . . . . . . . : 192.168.0.1
        Servery DNS . . . . . . . . . . . : 192.168.0.1
        Primární server WINS. . . . . . . : 192.168.0.1
        Zapůjčeno . . . . . . . . . . . . : 9. září 2011 16:05:32
        Zápůjčka vyprší . . . . . . . . . : 9. září 2011 20:05:32
如您所见,查找字符串“物理地址”有些徒劳,因为没有物理地址。但是,请注意,Windows有自己的MAC地址格式——每两个十六进制数字之间用连字符分隔(字母部分大写)。因此,寻找正则表达式:

([0-9A-F]{2}-){5}[0-9A-F]{2}
会给你你要找的MAC地址


警告:许多计算机有多个网络接口(有线和wifi、各种VPN等),因此输出中可能有多个MAC。

注意一些计算机,尤其是