Regular expression using sqlite in android

2019-07-13 10:05发布

I having a difficulty with regex, I have a table products that of one of it`s field is a period. I want to filter records by a period the user entered in my application. for example the period might be 2006-2012 or just 2006-

I want to filter all the records that contain only the pattern 2006-

I tried many examples but unfortunately nothing works properly

this is my code

Cursor c = rDB.rawQuery("select title,price from Products,UserProducts where UserProducts.product_code=Prodcuts.product_code and USER_EMAIL=? and Products.period like '%[0-9]+%' group by UserSeries.product_code order by Products.title asc", new String[]{email});
while(c.moveToNext())
{
         //save the records 
}

1条回答
该账号已被封号
2楼-- · 2019-07-13 11:02
Products.period like '%[0-9]+%'

LIKE does not work with regexps.

To use regular expression matching, use the REGEXP operator. Note that by default the REGEXP operator is not implemented, even though the syntax supports it.

For simpler matching needs you can also consider GLOB.

I want to filter all the records that contain only the pattern 2006-

You don't need a regular expression for that.

Products.period like '2006-%'

will match anything where period starts with the string 2006-.


I meant any year with "-" symbol at the end

Products.period like '%-'

will match anything where period ends with the string -.

查看更多
登录 后发表回答