Regex \b word boundary not works

扶醉桌前 提交于 2021-02-05 05:57:26

问题


In Android I have the next regexp \b(id)\b,

In this query (i.e.) I want replace exactly the word 'id' :

SELECT schedules.id as 'idreal' FROM schedules WHERE schedules.id = 12;

Final query:

SELECT schedules._id as 'idreal' FROM schedules WHERE schedules._id = 12;

But it doesn't work, the \b is for word boundary, but it doesn't work. What i'm doing groing?

This is my code:

Matcher matcher = Pattern.compile("\b(id)\b").matcher(field);
String query = matcher.replaceAll("_id");

Log.v(TAG, "Clean Query: "  + query);

I tested in

Thanks a lot.


回答1:


You should escape \ to represent \ literally. Otherwise \b means a backspace characters.

Matcher matcher = Pattern.compile("\\b(id)\\b").matcher(field);


来源:https://stackoverflow.com/questions/25938990/regex-b-word-boundary-not-works

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!