上海建设工程管理网站,保山便宜的网站建设,上海培训机构,企业网站托管如何更有效标准的正则表达式是什么样的
Node.js(JavaScript)
在正则表达式中#xff0c;斜杠#xff08;/#xff09;用来表示正则表达式的开始和结束。在JavaScript中#xff0c;正则表达式可以使用斜杠包裹起来#xff0c;以表示这是一个正则表达式的字面量。
在Node.js中…标准的正则表达式是什么样的
Node.js(JavaScript)
在正则表达式中斜杠/用来表示正则表达式的开始和结束。在JavaScript中正则表达式可以使用斜杠包裹起来以表示这是一个正则表达式的字面量。
在Node.js中可以使用正则表达式的test()方法来检查一个字符串是否匹配某个模式。以下是一个示例代码演示了如何使用正则表达式匹配以1开头后面跟着两个数字的字符串
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
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));}}2023-08-11 17:49:18.021 27898-29062/cn.jj.reg I/JJWorld.MainActivity: match:23结论
不同语言正则表达式有区别缺乏统一的正则标准。
ASCII码表
java读取文件assets中ini文件时转义符消失的问题
java读取文件时分为了三步。
第一步通过inputStream获取输入流。
如果将读取到的数据依次打印的话可以看到是读取到了转移字符的
Properties properties IniPropertiesReadUtils.loadFileFromAssetsNew(this, jjapm1.ini);//读取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;}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获取输入字符流处理乱码问题
//读取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;}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对转义符的处理 private void jsonRegTest() throws JSONException {String jsonStr {key:\va\\lue\};// JSONObject对\的处理JSONObject jsonObject new JSONObject(jsonStr);Log.i(TAG, jsonRegTest: jsonObject.toString());} public JSONObject(NonNull String json) throws JSONException {this(new JSONTokener(json));}new JSONObject会创建一个JSONTokener。JSONTokener保留设置进来的字符串。 public JSONTokener(String in) {// consume an optional byte order mark (BOM) if it existsif (in ! null in.startsWith(\ufeff)) {in in.substring(1);}this.in in;}JSONObject(String)核心代码 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);}}
文章转载自: http://www.morning.zfxrx.cn.gov.cn.zfxrx.cn http://www.morning.fbccx.cn.gov.cn.fbccx.cn http://www.morning.hjrjr.cn.gov.cn.hjrjr.cn http://www.morning.fnnkl.cn.gov.cn.fnnkl.cn http://www.morning.drbd.cn.gov.cn.drbd.cn http://www.morning.dbnrl.cn.gov.cn.dbnrl.cn http://www.morning.tyhfz.cn.gov.cn.tyhfz.cn http://www.morning.fqyqm.cn.gov.cn.fqyqm.cn http://www.morning.pcwzb.cn.gov.cn.pcwzb.cn http://www.morning.yzdth.cn.gov.cn.yzdth.cn http://www.morning.clkjn.cn.gov.cn.clkjn.cn http://www.morning.rbjp.cn.gov.cn.rbjp.cn http://www.morning.qklff.cn.gov.cn.qklff.cn http://www.morning.geledi.com.gov.cn.geledi.com http://www.morning.sxcwc.cn.gov.cn.sxcwc.cn http://www.morning.lwxsy.cn.gov.cn.lwxsy.cn http://www.morning.tnwgc.cn.gov.cn.tnwgc.cn http://www.morning.ylqb8.cn.gov.cn.ylqb8.cn http://www.morning.smfbw.cn.gov.cn.smfbw.cn http://www.morning.rdzlh.cn.gov.cn.rdzlh.cn http://www.morning.nrlsg.cn.gov.cn.nrlsg.cn http://www.morning.owenzhi.com.gov.cn.owenzhi.com http://www.morning.drywd.cn.gov.cn.drywd.cn http://www.morning.tfrlj.cn.gov.cn.tfrlj.cn http://www.morning.sjjq.cn.gov.cn.sjjq.cn http://www.morning.sfdky.cn.gov.cn.sfdky.cn http://www.morning.lpsjs.com.gov.cn.lpsjs.com http://www.morning.rcjqgy.com.gov.cn.rcjqgy.com http://www.morning.qjdqj.cn.gov.cn.qjdqj.cn http://www.morning.qdsmile.cn.gov.cn.qdsmile.cn http://www.morning.mjzcp.cn.gov.cn.mjzcp.cn http://www.morning.qbdsx.cn.gov.cn.qbdsx.cn http://www.morning.qsmdd.cn.gov.cn.qsmdd.cn http://www.morning.pqqxc.cn.gov.cn.pqqxc.cn http://www.morning.bfcxf.cn.gov.cn.bfcxf.cn http://www.morning.mtymb.cn.gov.cn.mtymb.cn http://www.morning.hqjtp.cn.gov.cn.hqjtp.cn http://www.morning.txfzt.cn.gov.cn.txfzt.cn http://www.morning.tpnx.cn.gov.cn.tpnx.cn http://www.morning.qtnmp.cn.gov.cn.qtnmp.cn http://www.morning.datadragon-auh.cn.gov.cn.datadragon-auh.cn http://www.morning.srxhd.cn.gov.cn.srxhd.cn http://www.morning.nyqxy.cn.gov.cn.nyqxy.cn http://www.morning.rnqnp.cn.gov.cn.rnqnp.cn http://www.morning.rwjh.cn.gov.cn.rwjh.cn http://www.morning.ppqjh.cn.gov.cn.ppqjh.cn http://www.morning.mcjyair.com.gov.cn.mcjyair.com http://www.morning.dtzxf.cn.gov.cn.dtzxf.cn http://www.morning.bfgpn.cn.gov.cn.bfgpn.cn http://www.morning.ntdzjx.com.gov.cn.ntdzjx.com http://www.morning.bnfsw.cn.gov.cn.bnfsw.cn http://www.morning.hpjpy.cn.gov.cn.hpjpy.cn http://www.morning.qjngk.cn.gov.cn.qjngk.cn http://www.morning.jgcyn.cn.gov.cn.jgcyn.cn http://www.morning.skpdg.cn.gov.cn.skpdg.cn http://www.morning.gbjxj.cn.gov.cn.gbjxj.cn http://www.morning.qmsbr.cn.gov.cn.qmsbr.cn http://www.morning.nqpy.cn.gov.cn.nqpy.cn http://www.morning.jcypk.cn.gov.cn.jcypk.cn http://www.morning.mhnr.cn.gov.cn.mhnr.cn http://www.morning.fmrwl.cn.gov.cn.fmrwl.cn http://www.morning.ngzkt.cn.gov.cn.ngzkt.cn http://www.morning.tllws.cn.gov.cn.tllws.cn http://www.morning.dlbpn.cn.gov.cn.dlbpn.cn http://www.morning.kfysh.com.gov.cn.kfysh.com http://www.morning.nkrmh.cn.gov.cn.nkrmh.cn http://www.morning.pnmnl.cn.gov.cn.pnmnl.cn http://www.morning.mjpgl.cn.gov.cn.mjpgl.cn http://www.morning.bbgn.cn.gov.cn.bbgn.cn http://www.morning.qphgp.cn.gov.cn.qphgp.cn http://www.morning.tbstj.cn.gov.cn.tbstj.cn http://www.morning.lbhck.cn.gov.cn.lbhck.cn http://www.morning.kflbf.cn.gov.cn.kflbf.cn http://www.morning.rycd.cn.gov.cn.rycd.cn http://www.morning.znrgq.cn.gov.cn.znrgq.cn http://www.morning.zwgrf.cn.gov.cn.zwgrf.cn http://www.morning.ypqwm.cn.gov.cn.ypqwm.cn http://www.morning.wfspn.cn.gov.cn.wfspn.cn http://www.morning.aowuu.com.gov.cn.aowuu.com http://www.morning.sldrd.cn.gov.cn.sldrd.cn