Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

221
Views
Match substring with alphanumeric characters (ignoring other substrings) in postgres 11

I have following table in Postres

col1
A02BX
A02BX Other drugs for peptic ulcer and gastro-oesophageal reflux disease (GORD); A03AE Serotonin receptor antagonists

I would like to fetch a substring which is a combination of alphabets and numbers like 'A02BX'. The desired output is:

col1
A02BX
A02BX | A03AE 

I am unable to find any solution to this. Though I tried fetching alphanumeric string using following pattern. But is matching the whole string.

[a-zA-Z0-9]+

The output I am getting is the same as initial table:

A02BX
A02BX Other drugs for peptic ulcer and gastro-oesophageal reflux disease (GORD); A03AE Serotonin receptor antagonists
over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

You can extract all matches using REGEXP_MATCHES and a regular expression that matches alphanumeric substrings between word boundaries that contain at least one letter and at least one digit:

 SELECT REGEXP_MATCHES( 'A02BX Other drugs for peptic ulcer and gastro-oesophageal reflux disease (GORD); A03AE Serotonin receptor antagonists', '\y(?:[AZ]+\d|\d+[AZ])[A-Z0-9]*\y', 'g')

See an SQLFiddle .

Details

  • \y - a word boundary
  • (?:[AZ]+\d|\d+[AZ]) - either 1+ letters and a digit or 1+ digits and then a letter
  • [A-Z0-9]* - 0 or more letters or digits
  • \y - a word boundary.
over 4 years ago · Santiago Trujillo Report

0

Another solution:

select * from t;
                                                         col1                                                          
-----------------------------------------------------------------------------------------------------------------------
 A02BX
 A02BX Other drugs for peptic ulcer and gastro-oesophageal reflux disease (GORD); A03AE Serotonin receptor antagonists
(2 rows)

select col1, regexp_matches(col1, '(\w\d\d\w\w)+', 'g') from t;
                                                         col1                                                          | regexp_matches 
-----------------------------------------------------------------------------------------------------------------------+----------------
 A02BX                                                                                                                 | {A02BX}
 A02BX Other drugs for peptic ulcer and gastro-oesophageal reflux disease (GORD); A03AE Serotonin receptor antagonists | {A02BX}
 A02BX Other drugs for peptic ulcer and gastro-oesophageal reflux disease (GORD); A03AE Serotonin receptor antagonists | {A03AE}
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!