2

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 Answer 1

4
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 -.

Sign up to request clarification or add additional context in comments.

5 Comments

I meant any year with "-" symbol at the end, with regexp i am getting an exception exception android.database.sqlite.SQLiteException: near "REGEX": syntax error (code 1) and with glob i am not getting any records from the cursor
REGEX is not REGEXP. But it's not supported anyway. GLOB pattern syntax is different and similar to LIKE in terms of expressive power. Updated the answer.
I checked my table again and strangely the periods contain ΓÇô and not - character
To me that looks like some other dash character UTF-8 octets interpreted as ISO-8859-x.
I tried many combinations with ΓÇô but nothing seems to work properly like '%ΓÇô' , like 'ΓÇô', in any case i don`t get any records

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.