java使用正则表达式时遇到的问题

标准的正则表达式是什么样的

Node.js(JavaScript)

在正则表达式中,斜杠(/)用来表示正则表达式的开始和结束。在JavaScript中,正则表达式可以使用斜杠包裹起来,以表示这是一个正则表达式的字面量。

在Node.js中,可以使用正则表达式的test()方法来检查一个字符串是否匹配某个模式。以下是一个示例代码,演示了如何使用正则表达式匹配以1开头,后面跟着两个数字的字符串:

java 复制代码
const str = '123';
const regex = /^1\d{2}$/;

if (regex.test(str)) {
  console.log('匹配成功');
} else {
  console.log('匹配失败');
}

在上面的示例中,^1\d{2}$是一个正则表达式,它的含义是:

^:匹配字符串的开头

1:匹配字符1

\d{2}:匹配两个数字(\d表示任意数字,{2}表示匹配两次)

$:匹配字符串的结尾

因此,该正则表达式可以匹配以1开头,后面跟着两个数字的字符串。在示例中,str的值为123,它符合这个模式,所以输出结果为匹配成功。

Java

java 复制代码
private void RegTest2() {
        String content ="abc$(abc(1.23(";
        //匹配(
        String regStr = "\\d{2}";
//        String regStr = "\\.";
        Pattern pattern = Pattern.compile(regStr);
        Matcher matcher = pattern.matcher(content);
        while (matcher.find()){
            Log.i(TAG,"match:" + matcher.group(0));
        }
    }
java 复制代码
2023-08-11 17:49:18.021 27898-29062/cn.jj.reg I/JJWorld.MainActivity: match:23

结论

不同语言正则表达式有区别,缺乏统一的正则标准。

ASCII码表

java读取文件assets中ini文件时,转义符消失的问题

java读取文件时,分为了三步。

第一步通过inputStream获取输入流。

如果将读取到的数据依次打印的话,可以看到是读取到了转移字符的

java 复制代码
Properties properties = IniPropertiesReadUtils.loadFileFromAssetsNew(this, "jjapm1.ini");
java 复制代码
     //读取Assets目录下的配置文件
    public static Properties loadFileFromAssetsNew(Context context, String filePath) {
        if (null == context || null == filePath || filePath.length() == 0)
            return null;

        InputStream assetFileIS = null;
        InputStreamReader assetFileISReader = null;
        Properties properties = new Properties();
        int readData = 0;
        try {
            assetFileIS = context.getResources().getAssets().open(filePath);
            while ((readData = assetFileIS.read())!= -1){
                Log.i(TAG,"read:" + (char)readData);
            }
        } catch (IOException e) {
            e.printStackTrace();
            properties = null;
        } finally {
            try {
                if (null != assetFileIS) {
                    assetFileIS.close();
                }
                if (null != assetFileISReader) {
                    assetFileISReader.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return properties;
    }
java 复制代码
2023-08-12 10:05:26.258 27428-27460/cn.jj.reg I/JJWorld.IniPropertiesReadUtils: read:c
2023-08-12 10:05:26.258 27428-27460/cn.jj.reg I/JJWorld.IniPropertiesReadUtils: read:l
2023-08-12 10:05:26.258 27428-27460/cn.jj.reg I/JJWorld.IniPropertiesReadUtils: read:a
2023-08-12 10:05:26.258 27428-27460/cn.jj.reg I/JJWorld.IniPropertiesReadUtils: read:s
2023-08-12 10:05:26.258 27428-27460/cn.jj.reg I/JJWorld.IniPropertiesReadUtils: read:s
2023-08-12 10:05:26.258 27428-27460/cn.jj.reg I/JJWorld.IniPropertiesReadUtils: read:R
2023-08-12 10:05:26.258 27428-27460/cn.jj.reg I/JJWorld.IniPropertiesReadUtils: read:u
2023-08-12 10:05:26.258 27428-27460/cn.jj.reg I/JJWorld.IniPropertiesReadUtils: read:l
2023-08-12 10:05:26.258 27428-27460/cn.jj.reg I/JJWorld.IniPropertiesReadUtils: read:e
2023-08-12 10:05:26.258 27428-27460/cn.jj.reg I/JJWorld.IniPropertiesReadUtils: read: 
2023-08-12 10:05:26.258 27428-27460/cn.jj.reg I/JJWorld.IniPropertiesReadUtils: read:=
2023-08-12 10:05:26.258 27428-27460/cn.jj.reg I/JJWorld.IniPropertiesReadUtils: read: 
2023-08-12 10:05:26.258 27428-27460/cn.jj.reg I/JJWorld.IniPropertiesReadUtils: read:ä
2023-08-12 10:05:26.258 27428-27460/cn.jj.reg I/JJWorld.IniPropertiesReadUtils: read:¸
2023-08-12 10:05:26.258 27428-27460/cn.jj.reg I/JJWorld.IniPropertiesReadUtils: read:­
2023-08-12 10:05:26.258 27428-27460/cn.jj.reg I/JJWorld.IniPropertiesReadUtils: read:{
2023-08-12 10:05:26.258 27428-27460/cn.jj.reg I/JJWorld.IniPropertiesReadUtils: read:\
2023-08-12 10:05:26.258 27428-27460/cn.jj.reg I/JJWorld.IniPropertiesReadUtils: read:d
2023-08-12 10:05:26.258 27428-27460/cn.jj.reg I/JJWorld.IniPropertiesReadUtils: read:\
2023-08-12 10:05:26.258 27428-27460/cn.jj.reg I/JJWorld.IniPropertiesReadUtils: read:d
2023-08-12 10:05:26.258 27428-27460/cn.jj.reg I/JJWorld.IniPropertiesReadUtils: read:"
2023-08-12 10:05:26.258 27428-27460/cn.jj.reg I/JJWorld.IniPropertiesReadUtils: read:}

上面打印的控制信息不管,可见是打印出来了classRule = 中{\d\d"}的,只不过中文是乱码。

第二步 通过InputStreamReader获取输入字符流,处理乱码问题

java 复制代码
//读取Assets目录下的配置文件
    public static Properties loadFileFromAssetsNew(Context context, String filePath) {
        if (null == context || null == filePath || filePath.length() == 0)
            return null;

        InputStream assetFileIS = null;
        InputStreamReader assetFileISReader = null;
        Properties properties = new Properties();
        int readData = 0;
        try {
            assetFileIS = context.getResources().getAssets().open(filePath);
//            while ((readData = assetFileIS.read())!= -1){
//                Log.i(TAG,"read:" + (char)readData);
//            }
            assetFileISReader = new InputStreamReader(assetFileIS, "UTF-8");
            while ((readData = assetFileISReader.read())!= -1){
                Log.i(TAG,"read11:" + (char)readData);
            }
        } catch (IOException e) {
            e.printStackTrace();
            properties = null;
        } finally {
            try {
                if (null != assetFileIS) {
                    assetFileIS.close();
                }
                if (null != assetFileISReader) {
                    assetFileISReader.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return properties;
    }
java 复制代码
2023-08-12 10:06:52.827 27565-27600/cn.jj.reg I/JJWorld.IniPropertiesReadUtils: read11:c
2023-08-12 10:06:52.827 27565-27600/cn.jj.reg I/JJWorld.IniPropertiesReadUtils: read11:l
2023-08-12 10:06:52.827 27565-27600/cn.jj.reg I/JJWorld.IniPropertiesReadUtils: read11:a
2023-08-12 10:06:52.827 27565-27600/cn.jj.reg I/JJWorld.IniPropertiesReadUtils: read11:s
2023-08-12 10:06:52.827 27565-27600/cn.jj.reg I/JJWorld.IniPropertiesReadUtils: read11:s
2023-08-12 10:06:52.827 27565-27600/cn.jj.reg I/JJWorld.IniPropertiesReadUtils: read11:R
2023-08-12 10:06:52.827 27565-27600/cn.jj.reg I/JJWorld.IniPropertiesReadUtils: read11:u
2023-08-12 10:06:52.827 27565-27600/cn.jj.reg I/JJWorld.IniPropertiesReadUtils: read11:l
2023-08-12 10:06:52.827 27565-27600/cn.jj.reg I/JJWorld.IniPropertiesReadUtils: read11:e
2023-08-12 10:06:52.827 27565-27600/cn.jj.reg I/JJWorld.IniPropertiesReadUtils: read11: 
2023-08-12 10:06:52.827 27565-27600/cn.jj.reg I/JJWorld.IniPropertiesReadUtils: read11:=
2023-08-12 10:06:52.827 27565-27600/cn.jj.reg I/JJWorld.IniPropertiesReadUtils: read11: 
2023-08-12 10:06:52.827 27565-27600/cn.jj.reg I/JJWorld.IniPropertiesReadUtils: read11:中
2023-08-12 10:06:52.827 27565-27600/cn.jj.reg I/JJWorld.IniPropertiesReadUtils: read11:{
2023-08-12 10:06:52.827 27565-27600/cn.jj.reg I/JJWorld.IniPropertiesReadUtils: read11:\
2023-08-12 10:06:52.827 27565-27600/cn.jj.reg I/JJWorld.IniPropertiesReadUtils: read11:d
2023-08-12 10:06:52.827 27565-27600/cn.jj.reg I/JJWorld.IniPropertiesReadUtils: read11:\
2023-08-12 10:06:52.827 27565-27600/cn.jj.reg I/JJWorld.IniPropertiesReadUtils: read11:d
2023-08-12 10:06:52.827 27565-27600/cn.jj.reg I/JJWorld.IniPropertiesReadUtils: read11:"
2023-08-12 10:06:52.827 27565-27600/cn.jj.reg I/JJWorld.IniPropertiesReadUtils: read11:}

转义符依然是存在的,classRule = 中{\d\d"}

第三步 properties.load(assetFileISReader)对转义符进行了特殊处理,导致转义符失效




JSONObject对转义符的处理

java 复制代码
    private void jsonRegTest() throws JSONException {
        String jsonStr = "{key:\"va\\lue\"}";
        // JSONObject对\的处理
        JSONObject jsonObject = new JSONObject(jsonStr);
        Log.i(TAG, "jsonRegTest:" + jsonObject.toString());
    }
java 复制代码
    public JSONObject(@NonNull String json) throws JSONException {
        this(new JSONTokener(json));
    }

new JSONObject会创建一个JSONTokener。JSONTokener保留设置进来的字符串。

java 复制代码
    public JSONTokener(String in) {
        // consume an optional byte order mark (BOM) if it exists
        if (in != null && in.startsWith("\ufeff")) {
            in = in.substring(1);
        }
        this.in = in;
    }

JSONObject(String)核心代码

java 复制代码
    public JSONObject(@NonNull JSONTokener readFrom) throws JSONException {
        /*
         * Getting the parser to populate this could get tricky. Instead, just
         * parse to temporary JSONObject and then steal the data from that.
         */
        Object object = readFrom.nextValue();
        if (object instanceof JSONObject) {
            this.nameValuePairs = ((JSONObject) object).nameValuePairs;
        } else {
            throw JSON.typeMismatch(object, "JSONObject");
        }
    }
相关推荐
爱上语文1 分钟前
Springboot的三层架构
java·开发语言·spring boot·后端·spring
serve the people5 分钟前
springboot 单独新建一个文件实时写数据,当文件大于100M时按照日期时间做文件名进行归档
java·spring boot·后端
qmx_071 小时前
HTB-Jerry(tomcat war文件、msfvenom)
java·web安全·网络安全·tomcat
为风而战1 小时前
IIS+Ngnix+Tomcat 部署网站 用IIS实现反向代理
java·tomcat
编程零零七2 小时前
Python数据分析工具(三):pymssql的用法
开发语言·前端·数据库·python·oracle·数据分析·pymssql
技术无疆3 小时前
快速开发与维护:探索 AndroidAnnotations
android·java·android studio·android-studio·androidx·代码注入
2401_858286113 小时前
52.【C语言】 字符函数和字符串函数(strcat函数)
c语言·开发语言
铁松溜达py3 小时前
编译器/工具链环境:GCC vs LLVM/Clang,MSVCRT vs UCRT
开发语言·网络
everyStudy3 小时前
JavaScript如何判断输入的是空格
开发语言·javascript·ecmascript
C-SDN花园GGbond5 小时前
【探索数据结构与算法】插入排序:原理、实现与分析(图文详解)
c语言·开发语言·数据结构·排序算法