3

Using linq (.net 3.5 +) and predicate builder, I did it like so:

var startsWith3Chars = new System.Text.RegularExpressions.Regex(@"^[a-zA-Z]{3}\-", System.Text.RegularExpressions.RegexOptions.Compiled);
wherePredicate = wherePredicate.And(x => startsWith3Chars.Matches(x.MATERIALUID).Count > 0);

But now I have the need to do this filtering within the command text.

Is there a way to use something like REGEXP_INSTR to limit the results based on a regular expression?

2 Answers 2

2

Given this test data ...

SQL> select name
  2  from t23
  3  /

NAME
----------
SAM-I-AM
MR KNOX
X11
CAT
LORAX

SQL>

... the following query uses REGEXP_LIKE() to return records whose first four characters contain only letters or hyphens:

SQL> select name
  2  from t23
  3  where regexp_like(name, '^[[:alpha:]\-]{4}')
  4  /

NAME
----------
SAM-I-AM
LORAX

SQL>

We can also use REGEXP_INSTR() with the same basic pattern (I have dropped the leading caret):

SQL> select name
  2  from t23
  3  where regexp_instr(name, '[[:alpha:]\-]{4}', 1) = 1
  4  /

NAME
----------
SAM-I-AM
LORAX

SQL>

Oracle added full regular expression support to its SQL in version 10g. Find out more.

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

1 Comment

Unfortunately working with an older version of Oracle. Thanks though, exactly what I was looking for.
2

How about REGEXP_LIKE?

2 Comments

APC sort of 1 up-ed you with an example. Thanks for responding.
Not a problem - if APC has the time to provide the example, then the credit should go there. If you would have needed the info in a hurry then what I provided would have got you there. It's all about solutions, not scoring.

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.