【背景】
比如对于宏定义:
#define get_var_3_para(d,e,f) _get_dev_var_value((d),(e),METHODID(f)) GET_DEV_VAR_VALUE("111 get dev var 111", count); |
想要在android中写java的正则去实现参数的替换。
分别将对应的
get_var_3_para宏定义中
_get_dev_var_value((d),(e),METHODID(f))
的d,e,f,换为对应的参数:
"para 1", "para 2", "para 3
【解决过程】
1.目前已经参考之前自己的:
【已解决】Java中的正则表达式(java.util.regex)的替换
【已解决】Java的正则表达式Regex中,如何查找所有的匹配的项
【已解决】Java的正则表达式java.util.regex中的命名的组(named group)
【已解决】Java的正则表达式java.util.regex中匹配星号’*’ asterisk字符本身
和
http://docs.oracle.com/javase/6/docs/api/java/util/regex/Pattern.html
实现了如下效果:
public String processDefineContent(String defineContent)
{
//use regex to process it
String processedDefineContent = defineContent; //_get_dev_var_value((d),(e),METHODID(f))
Pattern idP = Pattern.compile("([_a-zA-Z]\\w*)");
Matcher foundId = idP.matcher(processedDefineContent);
// Find all matches
while (foundId.find()) {
// Get the matching string
int curIdStartPos = foundId.start();
int curIdEndPos = foundId.end();
String strId = foundId.group();
System.out.println("[" + curIdStartPos + "-" + curIdEndPos + "]=" + strId);
/*
[0-18]=_get_dev_var_value
[20-21]=d
[24-25]=e
[27-35]=METHODID
[36-37]=f
*/
}
return processedDefineContent;
}2.然后此处为了,避免字符串中的id:
"…id…"
以及函数调用:
someFunc(…)
中的someFunc其实不是id,所以再去改代码。
先去支持去除字符串中的id,所以,直接先去匹配字符串,然后不处理即可。
加上测试内容:
#define get_var_3_para(d,e,f) _get_dev_var_value((d),(e),METHODID(f), "inside_id_should_not_match") GET_DEV_VAR_VALUE("111 get dev var 111", count); |
代码为:
public String processDefineContent(String defineContent)
{
//use regex to process it
String processedDefineContent = defineContent; //_get_dev_var_value((d),(e),METHODID(f), "inside_id_should_not_match")
Pattern idP = Pattern.compile("([_a-zA-Z]\\w*)|(\"[^\"]+?\")");
Matcher foundId = idP.matcher(processedDefineContent);
// Find all matches
while (foundId.find()) {
// Get the matching string
int curIdStartPos = foundId.start();
int curIdEndPos = foundId.end();
String strId = foundId.group();
System.out.println("[" + curIdStartPos + "-" + curIdEndPos + "]=" + strId);
/*
[0-18]=_get_dev_var_value
[20-21]=d
[24-25]=e
[27-35]=METHODID
[36-37]=f
[40-68]="inside_id_should_not_match"
*/
}
return processedDefineContent;
}3. 然后想要再去试试,加上对应的named group,以方便判断是ID还是字符串。
但是之前就以及知道了,java7中,才开始支持named group,所以要先确认此处,android中的java,是否是java7以上的,是否支持named group。
然后发现是支持的。
代码和输出如下:
public String processDefineContent(String defineContent)
{
//use regex to process it
String processedDefineContent = defineContent; //_get_dev_var_value((d),(e),METHODID(f), "inside_id_should_not_match")
Pattern idP = Pattern.compile("(?<id>[_a-zA-Z]\\w*)|(?<str>\"[^\"]+?\")");
Matcher foundId = idP.matcher(processedDefineContent);
// Find all matches
while (foundId.find()) {
// Get the matching string
int matchedIdStartPos = foundId.start(0);
int matchedIdEndPos = foundId.end(0);
String strMatchedId = foundId.group("id");
if(null != strMatchedId)
{
System.out.println("Is ID: [" + matchedIdStartPos + "-" + matchedIdEndPos + "]=" + strMatchedId);
}
else
{
int matchedStrStartPos = foundId.start(0);
int matchedStrEndPos = foundId.end(0);
String strMatchedStr = foundId.group("str");
System.out.println("Is String: [" + matchedIdStartPos + "-" + matchedIdEndPos + "]=" + strMatchedStr);
}
/*
Is ID: [0-18]=_get_dev_var_value
Is ID: [20-21]=d
Is ID: [24-25]=e
Is ID: [27-35]=METHODID
Is ID: [36-37]=f
Is String: [40-68]="inside_id_should_not_match"
*/
}
return processedDefineContent;
}4.后来又自动忽略掉:
someFunc(xxx
类型的ID,
用的代码和输出如下:
public String processDefineContent(String defineContent)
{
//use regex to process it
String processedDefineContent = defineContent; //_get_dev_var_value((d),(e),METHODID(f), "inside_id_should_not_match")
//Pattern idP = Pattern.compile("((?<id>[_a-zA-Z]\\w*+)(?!\\())|(?<str>\"[^\"]+?\")");
//Pattern idP = Pattern.compile("(([_a-zA-Z]\\w*+)(?!\\())|(?:\"[^\"]+?\")");
Pattern idP = Pattern.compile("((?<id>[_a-zA-Z]\\w*+)(?!\\())|(?<str>\"[^\"]+?\")"); //auto omit "someFunc(xxx" type id
Matcher foundId = idP.matcher(processedDefineContent);
// Find all matches
while (foundId.find()) {
// Get the matching string
int matchedIdStartPos = foundId.start(0);
int matchedIdEndPos = foundId.end(0);
String strMatchedId = foundId.group("id");
if(null != strMatchedId)
{
System.out.println("Is ID: [" + matchedIdStartPos + "-" + matchedIdEndPos + "]=" + strMatchedId);
}
else
{
int matchedStrStartPos = foundId.start(0);
int matchedStrEndPos = foundId.end(0);
String strMatchedStr = foundId.group("str");
System.out.println("Is String: [" + matchedIdStartPos + "-" + matchedIdEndPos + "]=" + strMatchedStr);
}
/*
Is ID: [20-21]=d
Is ID: [24-25]=e
Is ID: [36-37]=f
Is String: [40-68]="inside_id_should_not_match"
*/
}
return processedDefineContent;
}
【总结】
android上的java的正则,默认已经是java7之后的了。
支持named group,还不错了。
只是java本身的正则,还是相对比较弱,和不太好用的。。。。
转载请注明:在路上 » 【记录】Android中用java的正则查找并替换宏定义中的参数