1

I would like to have a regular expression to make an Oracle SQL REGEXP_LIKE query that checks

  • if a string starts with one alphanumeric character
  • if the string ends with one alphanumeric character

  • if the "body" of the string contains only alphanumeric character OR these authorized characters (written) : hyphen (dash), dot, apostrophe, space

  • if the authorised characters are NOT adjacent (to avoid something like "he--'''l..'-lo")

I started with this :

^[a-zA-Z0-9]+(a-zA-Z0-9\-\.'|([^\-\.'])\1)*[a-zA-Z0-9]$

I used backslash to escape assuming that dot and hyphen are metacharacters

1
  • There would be value in you updating your question to clarify if you authorize the character, _. Also, while your requirement is not bad, sample data with an indication of true or false would not hurt. Oct 13, 2015 at 16:37

2 Answers 2

0

I think this is what you want:

^[a-zA-Z0-9]+([-.' ][a-zA-Z0-9]|[a-zA-Z0-9])*\w?$

It looks for

  • at least 1 alphanumeric (alnum),

followed by

  • either an authorized character followed by an alphanumeric or just an alphanumeric, repeated any number of times (including 0).

optionally followed by

  • an alnum

This meets your specification. I'm not sure if starts with one alnum and ends with one alnum means that there must be at least 2 alnums, or if they can be the same. If there must be at least 2 of them, remove the last ? (which make the last alnum optional).

Regards

5
  • You are geniuses. Thank you both. Last question, how to obtain anything but this expression? (inverse) Oct 13, 2015 at 11:24
  • I mean it returns every occurences which doesn't start with an alphanum OR doesn't end with an alphanum OR having two adjacent authorized characters OR with unauthorized character anywhere Oct 13, 2015 at 11:31
  • The last \w has to be [:alnum:] (\w matches also the underscore _) ([:alnum:] may substitute also the first [a-zA-Z0-9]) Oct 13, 2015 at 12:03
  • If I understand you correctly you want to capture the whole expression. If oracle's regex flavor supports non capturing groups - make the repeat non capturing and place a capture group around the whole expression - ^([a-zA-Z0-9]+(?:[-.' ][a-zA-Z0-9]|[a-zA-Z0-9])*[a-zA-Z0-9]?)$.
    – SamWhan
    Oct 13, 2015 at 13:00
  • 1
    @GsusRecovery Right. When I experimented with the RE i used \w which i then replaced with [a-zA-Z0-9] because I didn't know if oracle could handle \w. Then I forgot the last one :P... But as you say, if _ shouldn't be allowed, it should be changed.
    – SamWhan
    Oct 13, 2015 at 13:05
0

assuming you meant "authorised characters are NOT adjacent to each other"

try something along these lines

^[a-zA-Z0-9]+([a-zA-Z0-9]+[\-\.' ]?)*[a-zA-Z0-9]$

so that the repeating middle part always has one alphanumeric character followed by zero to one special characters.

1
  • Posted an alternative since this one requires at least three alphanumerics which wasn't in the specification. But writing this comment I realize that your regex could be made to work by removing the first alphanumeric test i.e. ^([a-zA-Z0-9]+[\-\.' ]?)*[a-zA-Z0-9]$.Note the last paragraph in my answer though. (However here you'd have to change * to a ? to have at least 2 alnums.)
    – SamWhan
    Oct 13, 2015 at 10:22

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.