0

I have a query which I was using in an Access database to match a field. The rows I wish to retrieve have a field which contains a sequence of characters in two possible forms (case-insensitive):

  • *PO12345, 5 digits preceded by *PO, or
  • PO12345, 5 digits preceded by PO.

In Access I achieved this with:

WHERE MyField LIKE '*PO#####*'

I have tried to replicate the query for use in an Oracle database:

WHERE REGEXP_LIKE(MyField, '/\*+PO[\d]{5}/i')

However, it doesn't return anything. I have tinkered with the Regex slightly, such as placing brackets around PO, but to no avail. To my knowledge what I have written is correct.

4
  • There are five digits. Commented Mar 9, 2016 at 16:50
  • I think it should be WHERE REGEXP_LIKE(MyField, '\*?PO\d{6}') Commented Mar 9, 2016 at 16:52
  • I've just edited the question - I should've said 5 digits in the question. Pardon my haste. Commented Mar 9, 2016 at 16:52
  • If there are more than 5 digits, should the string still match the regex? Commented Mar 10, 2016 at 15:41

1 Answer 1

4

Your regex \*+PO[\d]{5} is wrong. There shouldn't be + after \* as it's optional.

Using ? like this /\*?PO\d{5}/i solves the problem.

Use i (case insensitive) as parameter like this: REGEXP_LIKE (MyField, '^\*?PO\d{5}$', 'i');

Regex101 Demo

Read REGEXP_LIKE documentation.

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

6 Comments

Yes, sorry I don't know why I was using + instead of ?; however, it still returns nothing.
I would add start and end anchors too, so REGEXP_LIKE(myfield, '^\*?PO\d{5}$', 'i') - otherwise the regex will match strings like ABCD*PO1234567890, etc.
@DavidFaber *PO12345 is within the field somewhere; it does not make up the whole field.
@noob thank you - it works now. I initially missed one of your more subtle changes: [\d] to \d.
@monster, thanks for the correction - I did not read the entire question carefully. If that be the case then the * can be omitted entirely in the regex since PO\d{5} matches *PO12345
|

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.