Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex \b word boundary not works

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.

like image 471
Ollie Strevel Avatar asked Oct 23 '25 01:10

Ollie Strevel


1 Answers

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

Matcher matcher = Pattern.compile("\\b(id)\\b").matcher(field);
like image 165
falsetru Avatar answered Oct 25 '25 15:10

falsetru



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!