我使用以下查询来返回列中字符串的子集:
SELECT
REGEXP_EXTRACT(content, r'/\ :dependencies\ \[(.*?)\]]\ /g') AS deps
FROM x[my-test-162023:lab.clj_files_results_030904]
但返回以下内容:
Row deps
1 null
2 null
3 null
4 null
5 null
6 null
我已经在http://www.regexpal.com/和https://regex101.com/r/Gjre2i/2上测试了正则表达式模式,它似乎正在运行。
感谢任何帮助/提示。
更新 我尝试查询的表格如下所示:
Row content
1 (defproject spaghetti "0.1.0-SNAPSHOT" :description "FIXME: write description" :url "http://example.com/FIXME" :license {:name "Eclipse Public License" :url "http://www.eclipse.org/legal/epl-v10.html"} :source-paths ["src/clj" "src/cljs"] :dependencies [[org.clojure/clojure "1.6.0"] [org.clojure/clojurescript "0.0-2371" :scope "provided"] [org.clojure/core.async "0.1.346.0-17112a-alpha"] [ring "1.3.1"] [compojure "1.2.0"] [enlive "1.1.5"] [om "0.7.3"] [figwheel "0.1.4-SNAPSHOT"] [environ "1.0.0"] [com.cemerick/piggieback "0.1.3"] [weasel "0.4.3-SNAPSHOT"] [leiningen "2.5.0"] [http-kit "2.1.19"] [com.cognitect/transit-cljs "0.8.188"] ; [devcards "0.1.2-SNAPSHOT"] [sablono "0.2.22"] [prismatic/om-tools "0.3.3"]] :plugins [[lein-cljsbuild "1.0.3"] [lein-environ "1.0.0"]] :min-lein-version "2.5.0" :uberjar-name "spaghetti.jar" :cljsbuild {:builds {:app {:source-paths ["src/cljs/spaghetti"] :compiler {:output-to "resources/public/js/app.js" :output-dir "resources/public/js/out" :source-map "resources/public/js/out.js.map" :optimizations :none :preamble ["react/react.min.js" "public/js/adsr/index.js" "public/js/WebMIDIAPIWrapper/js/WebMIDIAPIWrapper.js" "public/js/hammerjs/hammer.min.js" "public/js/wavy-jones/wavy-jones.js"] :externs ["react/externs/react.js" "public/js/adsr/adsr.externs.js" "public/js/WebMIDIAPIWrapper/WebMIDIAPIWrapper.externs.js" "public/js/hammerjs/hammerjs.externs.js" "public/js/wavy-jonewavy-jones.externs.js"] :pretty-print true}}}} :profiles {:dev {:repl-options {:init-ns spaghetti.server :timeout 120000 :nrepl-middleware [cemerick.piggieback/wrap-cljs-repl]} :plugins [[lein-figwheel "0.1.4-SNAPSHOT"]] :figwheel {:http-server-root "public" :port 3449 :css-dirs ["resources/public/css"]} :env {:is-dev true} :cljsbuild {:builds {:app {:source-paths ["env/dev/cljs"]}}}} :uberjar {:hooks [leiningen.cljsbuild] :env {:production true} :omit-source true :aot :all :cljsbuild {:builds {:app {:source-paths ["env/prod/cljs"] :compiler {:optimizations :advanced :pretty-print false }}}}}})
2 (defproject pomodoro "0.0.4" :license {:name "MIT" :url "http://opensource.org/licenses/MIT" :distribution :repo} :description "A simple pomodoro timer" :url "https://github.com/landau/cljs-pomodoro" :dependencies [[org.clojure/clojure "1.6.0"] [org.clojure/clojurescript "0.0-2322"] [org.clojure/core.async "0.1.338.0-5c5012-alpha"] [com.andrewmcveigh/cljs-time "0.1.6"] [reagent "0.4.2"]] :plugins [[lein-ring "0.8.11"] [lein-cljsbuild "1.0.3"] [lein-environ "0.5.0"]] :ring {:handler server.core/app} :profiles {:uberjar {:aot :all} :dev {:dependencies [[ring-mock "0.1.5"] [ring/ring-devel "1.3.0"] [compojure "1.1.9"]] :env {:dev true}} :release {:ring {:open-browser? false :stacktraces? false :auto-reload? false}}} :source-paths ["src"] :main server.core :cljsbuild { :builds [{:id "dev" :source-paths ["src-cljs"] :compiler {:output-to "public/js/pomodoro.js" :output-dir "public/js/dev" :optimizations :none :pretty-print tru :source-map true}} {:id "prod" :source-paths ["src-cljs"] :compiler {:output-to "public/js/main.js" :optimizations :advanced :pretty-print false :externs ["public/js/react-min-0.11.2.js"] }}]})
3 (defproject datascript-mori "0.15.2" :description "Wrapper for datascript interplay mori" :url "https://github.com/typeetfunc/datascript-mori" :license {:name "Eclipse Public License" :url "http://www.eclipse.org/legal/epl-v10.html"} :min-lein-version "2.5.3" :dependencies [[org.clojure/clojure "1.7.0"] [org.clojure/clojurescript "1.7.170"] [datascript "0.15.0"]] :plugins [[lein-cljsbuild "1.1.2" :exclusions [[org.clojure/clojure]]] [lein-git-deps "0.0.2-SNAPSHOT"]] :git-dependencies [["https://github.com/swannodette/mori.git"]] :source-paths ["src" ".lein-git-deps/mori/src"] :clean-targets ^{:protect false} ["target"] :cljsbuild {:builds [{:id "min" :source-paths ["src" ".lein-git-deps/mori/src"] :compiler { :output-to "release-js/datascript-mori.bare.js" :main datascript-mori.core :optimizations :advanced :pretty-print false } :notify-command ["release-js/wrap_bare.sh"]}]} )
4 (defproject pandect "0.6.1-SNAPSHOT" :description "Message Digest and Checksum Library for Clojure" :url "https://github.com/xsc/pandect" :license {:name "MIT License" :url "https://opensource.org/licenses/MIT" :year 2014 :key "mit"} :dependencies [[org.clojure/clojure "1.8.0" :scope "provided"] [org.bouncycastle/bcprov-jdk15on "1.54" :scope "provided"]] :exclusions [org.clojure/clojure] :source-paths ["src/clojure" "target/generated"] :java-source-paths ["src/java"] :profiles {:dev {:plugins [[lein-codox "0.9.4"]] :codox {:project {:name "pandect"} :metadata {:doc/format :markdown} :output-path "doc" :namespaces [pandect.core pandect.buffer #"^pandect\.algo\.[a-z\-]+"]}} :benchmark {:dependencies [[criterium "0.4.3"] [clj-message-digest "1.0.0"] [digest "1.4.4"]] :source-paths ["shootout"] :jvm-opts ^:replace ["-Xmx1g" "-server"]} :1.5 {:dependencies [[org.clojure/clojure "1.5.1"]]} :1.6 {:dependencies [[org.clojure/clojure "1.6.0"]]} :1.7 {:dependencies [[org.clojure/clojure "1.7.0"]]}} :prep-tasks ["codegen"] :aliases {"benchmark" ["with-profile" "dev,benchmark" "run" "-m"] "codegen" ["run" "-m" "pandect.codegen"] "all" ["with-profile" "+dev:+1.5:+1.6:+1.7"]} :pedantic? :abort)
5 (defproject stch-library/sql "0.1.1" :description "A DSL in Clojure for SQL query, DML, and DDL." :url "https://github.com/stch-library/sql" :license {:name "Eclipse Public License" :url "http://www.eclipse.org/legal/epl-v10.html"} :dependencies [[org.clojure/clojure "1.5.1"] [stch-library/schema "0.3.3"]] :profiles {:dev {:dependencies [[speclj "3.0.2"]]}} :plugins [[speclj "3.0.2"] [codox "0.6.7"]] :codox {:src-dir-uri "https://github.com/stch-library/sql/blob/master/" :src-linenum-anchor-prefix "L"} :test-paths ["spec"])
6 (defproject laboratory "0.1.0-SNAPSHOT" :description "do science in production" :url "https://github.com/yeller/laboratory" :license {:name "Eclipse Public License" :url "http://www.eclipse.org/legal/epl-v10.html"} :dependencies [[org.clojure/clojure "1.8.0"]] :profiles {:dev {:dependencies [[org.clojure/tools.namespace "0.2.4"]]} :benches {:dependencies [[criterium "0.4.1"]] :source-paths ["src" "benches"]}} :global-vars {*warn-on-reflection* true *unchecked-math* :warn-on-boxed ;*compiler-options* {:disable-locals-clearing true} *assert* true})
答案 0 :(得分:2)
/g
表示您要提取所有匹配项,而不仅仅是REGEXP_EXTRACT
所做的匹配项。
您应该使用REGEXP_EXTRACT_ALL
代替UNNEST
请尝试以下
#standardSQL
SELECT deps
FROM `my-test-23.lab.clj_project_files`,
UNNEST(REGEXP_EXTRACT_ALL(content, r' :dependencies \[(.*?)\]] ')) AS deps
得到"查询返回零记录" ....
根据您的示例尝试下面的虚拟数据(刚刚剥离前两行)
#standardSQL
WITH yourTable AS (
SELECT 1 AS id, '(defproject :dependencies [[org.clojure/clojure "1.6.0"] [org.clojure/clojurescript "0.0-2371" :scope "provided"] [org.clojure/core.async "0.1.346.0-17112a-alpha"] [ring "1.3.1"] [compojure "1.2.0"] [enlive "1.1.5"] [om "0.7.3"] [figwheel "0.1.4-SNAPSHOT"] [environ "1.0.0"] [com.cemerick/piggieback "0.1.3"] [weasel "0.4.3-SNAPSHOT"] [leiningen "2.5.0"] [http-kit "2.1.19"] [com.cognitect/transit-cljs "0.8.188"] ; [devcards "0.1.2-SNAPSHOT"] [sablono "0.2.22"] [prismatic/om-tools "0.3.3"]] :plugins [[lein-cljsbuild "1.0.3"] [lein-environ "1.0.0"]] ' AS content UNION ALL
SELECT 2, '(defproject :dependencies [[org.clojure/clojure "1.6.0"] [org.clojure/clojurescript "0.0-2322"] [org.clojure/core.async "0.1.338.0-5c5012-alpha"] [com.andrewmcveigh/cljs-time "0.1.6"] [reagent "0.4.2"]] :plugins [[lein-ring "0.8.11"] [lein-cljsbuild "1.0.3"] [lein-environ "0.5.0"]] :ring {:handler server.core/app} :profiles {:uberjar {:aot :all} :dev {:dependencies [[ring-mock "0.1.5"] [ring/ring-devel "1.3.0"] [compojure "1.1.9"]] :env {:dev true}} '
)
SELECT id, deps
FROM yourTable, UNNEST(SPLIT(REPLACE(
REGEXP_EXTRACT(content, r' :dependencies \[(\[.*?])*] ')
,'] [', '],['))) AS deps
ORDER BY id
结果如下
Row id deps
1 1 [org.clojure/clojurescript "0.0-2371" :scope "provided"]
2 1 [prismatic/om-tools "0.3.3"]
3 1 [sablono "0.2.22"]
4 1 [com.cognitect/transit-cljs "0.8.188"] ; [devcards "0.1.2-SNAPSHOT"]
5 1 [http-kit "2.1.19"]
6 1 [leiningen "2.5.0"]
7 1 [weasel "0.4.3-SNAPSHOT"]
8 1 [com.cemerick/piggieback "0.1.3"]
9 1 [environ "1.0.0"]
10 1 [figwheel "0.1.4-SNAPSHOT"]
11 1 [om "0.7.3"]
12 1 [enlive "1.1.5"]
13 1 [compojure "1.2.0"]
14 1 [ring "1.3.1"]
15 1 [org.clojure/core.async "0.1.346.0-17112a-alpha"]
16 1 [org.clojure/clojure "1.6.0"]
17 2 [org.clojure/clojure "1.6.0"]
18 2 [org.clojure/clojurescript "0.0-2322"]
19 2 [org.clojure/core.async "0.1.338.0-5c5012-alpha"]
20 2 [com.andrewmcveigh/cljs-time "0.1.6"]
21 2 [reagent "0.4.2"]
...但如果我将其删除并在我自己的数据集/表格中运行,我就不会得到任何结果......
看起来您的真实数据是"稍微"与你在问题中的显示方式不同
尝试以下 - 现在应该可以使用:o)
#standardSQL
SELECT id, deps
FROM `my-test-23.lab.clj_project_files`,
UNNEST(SPLIT(REGEXP_REPLACE(
REGEXP_EXTRACT(content, r'(?s) :dependencies \[(\[.*?])]')
, r']\n *\[', '],['))) AS deps
ORDER BY id