Hive如何用regexp_extract函数提取三重引号内的字符串
Extract Text Inside Triple Quotes Using Hive's
regexp_extract To get just the text inside the triple quotes (without including the quotes themselves), you need to adjust your regex to capture the inner content using a capturing group and reference that group instead of the full match.
Corrected Query
For your first example, the working query would be:
select '"""hello:world"""' as in_str, regexp_extract('"""hello:world"""', '"""(.*?)"""', 1) as out_str;
This will return hello:world as the output.
How It Works
Let’s break down the regex pattern """(.*?)""":
""": Matches the opening triple quotes exactly.(.*?): A capturing group that matches any character (.) zero or more times, but in a non-greedy way (?). This ensures we stop at the first occurrence of closing triple quotes instead of over-matching (critical if your data ever has multiple quote pairs in a single string).""": Matches the closing triple quotes exactly.- The third argument
1tellsregexp_extractto return the content of the first capturing group, not the entire matched string (which would be group0and include the quotes).
Testing with Your Second Example
For the string """abc:|:def""", the same pattern works perfectly:
select '"""abc:|:def"""' as in_str, regexp_extract('"""abc:|:def"""', '"""(.*?)"""', 1) as out_str;
Output: abc:|:def
Quick Tip
If you’re working with a table column instead of a literal string, just replace the literal with your column name:
select your_column as in_str, regexp_extract(your_column, '"""(.*?)"""', 1) as out_str from your_hive_table;
内容的提问来源于stack exchange,提问作者Regressor
相关产品推荐
相关产品推荐

