À quoi ressemble une expression régulière standard
Node.js(JavaScript)
Dans les expressions régulières, une barre oblique (/) est utilisée pour indiquer le début et la fin de l'expression régulière. En JavaScript, les expressions régulières peuvent être entourées de barres obliques pour indiquer qu'il s'agit d'une expression régulière littérale.
Dans Node.js, vous pouvez utiliser la méthode d'expression régulière test() pour vérifier si une chaîne correspond à un modèle. Voici un exemple de code qui montre comment utiliser une expression régulière pour faire correspondre une chaîne commençant par 1 suivi de deux nombres :
const str = '123';
const regex = /^1\d{
2}$/;
if (regex.test(str)) {
console.log('匹配成功');
} else {
console.log('匹配失败');
}
Dans l'exemple ci-dessus, ^1\d{2}$ est une expression régulière, ce qui signifie :
^ : correspond au début de la chaîne
1 : correspond au caractère 1
\d{2} : correspond à deux nombres (\d signifie n'importe quel nombre, {2} signifie correspondre deux fois)
$ : correspond à la fin de la chaîne
Cette expression régulière peut donc correspondre à une chaîne commençant par 1 suivi de deux chiffres. Dans l'exemple, la valeur de str est 123, ce qui correspond à ce modèle, le résultat de sortie est donc une correspondance réussie.
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
en conclusion
Il existe des différences dans les expressions régulières selon les langues et il existe un manque d’expressions régulières uniformes.
Table de codes ASCII
La façon de représenter légalement les caractères d'échappement dans les chaînes en Java
Lorsque Java lit le fichier ini dans le fichier Assets, le caractère d'échappement disparaît
Lorsque Java lit un fichier, celui-ci est divisé en trois étapes.
La première étape consiste à obtenir le flux d’entrée via inputStream.
Si vous imprimez les données lues dans l'ordre, vous pouvez voir que le caractère de transfert est lu
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:}
Quelles que soient les informations de contrôle imprimées ci-dessus, on peut voir qu'elles sont imprimées classRule = 中{\d\d"}
, mais les caractères chinois sont tronqués.
La deuxième étape consiste à obtenir le flux de caractères d'entrée via InputStreamReader et à résoudre le problème tronqué.
//读取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:}
Le caractère d'échappement existe toujours,classRule = 中{\d\d"}
La troisième étape Properties.load(assetFileISReader) a un traitement spécial pour le caractère d'échappement, ce qui rend le caractère d'échappement invalide.
Gestion des caractères d'échappement par 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));
}
le nouveau JSONObject crée un JSONTokener. JSONTokener conserve la chaîne entrante.
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;
}
Code de base 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");
}
}