android 宏替换_【记录】Android中用java的正则查找并替换宏定义中的参数

【背景】

比如对于宏定义:#define get_var_3_para(d,e,f)    _get_dev_var_value((d),(e),METHODID(f))

#define GET_DEV_VAR_VALUE(a,b)        get_var_3_para(a,0,b)

GET_DEV_VAR_VALUE("111 get dev var 111", count);

get_var_3_para("para 1", "para 2", "para 3");

想要在android中写java的正则去实现参数的替换。

分别将对应的

get_var_3_para宏定义中

_get_dev_var_value((d),(e),METHODID(f))

的d,e,f,换为对应的参数:

"para 1", "para 2", "para 3

【解决过程】

1.目前已经参考之前自己的:

实现了如下效果: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")

#define GET_DEV_VAR_VALUE(a,b)        get_var_3_para(a,0,b)

GET_DEV_VAR_VALUE("111 get dev var 111", count);

get_var_3_para("para 1", "para 2", "para 3");

代码为: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("(?[_a-zA-Z]\\w*)|(?\"[^\"]+?\")");

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("((?[_a-zA-Z]\\w*+)(?!\\())|(?\"[^\"]+?\")");

//Pattern idP = Pattern.compile("(([_a-zA-Z]\\w*+)(?!\\())|(?:\"[^\"]+?\")");

Pattern idP = Pattern.compile("((?[_a-zA-Z]\\w*+)(?!\\())|(?\"[^\"]+?\")"); //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本身的正则,还是相对比较弱,和不太好用的。。。。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值