Home > OS >  Select filename from file path : invalid perl operator: (?=
Select filename from file path : invalid perl operator: (?=

Time:03-30

I'm querying github on BigQuery. I'm trying to use the following regex expression to get the filename from a given file path, i.e.: given /src/components/App.vue it should return App:

SELECT regexp_extract(f.path, r'[A-Za-z0-9_\-\.] ?(?=\.)') as filename

This is giving the error stated in the question title. Is there a workaround I can use to achieve what I need?

CodePudding user response:

Try below instead

SELECT f.path, regexp_extract(f.path, r'/([^/.] ).[^.]*$') as filename    

if applied to sample in your question - output is

enter image description here

CodePudding user response:

Use

SELECT regexp_extract(f.path, r'([A-Za-z0-9_-] ?)[.]') as filename

See regex proof.

EXPLANATION

--------------------------------------------------------------------------------
  (                        group and capture to \1:
--------------------------------------------------------------------------------
    [A-Za-z0-9_-] ?          any character of: 'A' to 'Z', 'a' to
                             'z', '0' to '9', '_', '-' (1 or more
                             times (matching the least amount
                             possible))
--------------------------------------------------------------------------------
  )                        end of \1
--------------------------------------------------------------------------------
  [.]                      any character of: '.'
  • Related