You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Hive中使用reg_extract或split提取#后的首个非空字符串?解决reg_extract返回NULL的问题

Solution for Extracting First Non-# String in Hive

I’ve dealt with this exact regex quirk in Hive before—those leading hashes can throw off your pattern matching if you don’t account for them properly. Let’s get your extraction working.

Why Your reg_extract Returned NULL

I assume you meant regexp_extract here. The issue was likely your regex pattern not accounting for leading # characters. A simple [^#]+ without anchoring to the string start won’t target the first valid segment after those leading hashes correctly, leading to NULL results.

Working regexp_extract Implementation

Since Hive supports regexp_extract, we can build a regex that skips leading hashes and captures the first sequence of non-# characters. Here’s the code:

SELECT 
  regexp_extract(col_name, '^#*([^#]+)', 1) AS first_segment
FROM your_table;

Regex Breakdown:

  • ^: Anchors the match to the start of the string (critical for targeting the first valid segment)
  • #*: Matches 0 or more leading # characters (handles all cases: strings starting with no hashes, one hash, or multiple hashes)
  • ([^#]+): Captures the first sequence of one or more non-# characters (this is the value we want)
  • The final 1 tells Hive to return the first captured group

Testing with Your Sample Data

Let’s verify this against your records:

  • Input: ##214628##564#7576#7876 → Output: 214628
  • Input: #12771#242###256823 → Output: 12771
  • Input: ###3264###7236473####3 → Output: 3264

This matches your expected results exactly.

Optional: Convert to Numeric Type

If you need the result as a number (since your sample outputs are integers), wrap the extraction in cast() (older Hive versions may not support to_number):

SELECT 
  cast(regexp_extract(col_name, '^#*([^#]+)', 1) AS bigint) AS first_segment_num
FROM your_table;

内容的提问来源于stack exchange,提问作者Pikasa Bagchi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.06 06:53:28