如何使用tableau中的正则表达式提取确切的单词?

时间:2015-10-07 06:59:03

标签: regex tableau

[{"subscription_id":"2","service_id":"2","service_name":"product1","service_category":"sms","rate_type":"high","rate_amount":101.0}]

我想提取" product1"从这个字符串,无法为这个需要帮助写出正确的表达式。

2 个答案:

答案 0 :(得分:0)

你去。

Regex101.com演示here

-- create usage table create table `usage` ( job varchar(100), start_time date, end_time date, `session` varchar(100) ); -- create `sessions` table create table `sessions` ( session_label varchar(100) , start_time date, end_time date ); -- create procedure prc_insert_into_usage delimiter $$ Create Procedure prc_insert_into_usage ( IN p_job varchar(100), IN p_start_time date, IN p_end_time date ) Begin Declare v_session varchar(100); -- check if record is there in the session table -- it is assumed session table will have only one record for composite key (start_time, end_time) Select session_label into v_session from `sessions` where start_time = p_start_time and end_time = p_end_time; IF (v_session IS NULL ) THEN -- if session_label is generated using auto increment key in sessions table then -- last_insert_id() can be used to fetch that value after insert into sessions table -- which can be used while inserting into usage table -- example below -- Insert into `sessions` (start_time,end_time) values (p_start_time,p_end_time); -- set @v_session = last_insert_id(); -- Insert into `usage` values (p_job,p_start_time,p_end_time,@v_session); -- dummy logic to create the session_label -- dummy logic is used you can replace it with whatever logic you need set @v_session = concat('sess_',left(uuid(),8)); -- insert record in both table (first in session table and then in usage table) Insert into `sessions` values (@v_session,p_start_time,p_end_time); Insert into `usage` values (p_job,p_start_time,p_end_time,@v_session); else -- record already present in sessions table for this session label -- so insert record only in usage table Insert into `usage` values (p_job,p_start_time,p_end_time,@v_session); End If; End; $$ -- call this procedure to insert data into usage table -- sample execution below when both tables have no rows -- below call will insert 1 rows in both table having same session label mysql> call prc_insert_into_usage('job_a','2015-01-01','2015-01-02'); Query OK, 1 row affected (0.00 sec) mysql> select * from `usage`; +-------+------------+------------+---------------+ | job | start_time | end_time | session | +-------+------------+------------+---------------+ | job_a | 2015-01-01 | 2015-01-02 | sess_abc376bf | +-------+------------+------------+---------------+ 1 row in set (0.00 sec) mysql> select * from `sessions`; +---------------+------------+------------+ | session_label | start_time | end_time | +---------------+------------+------------+ | sess_abc376bf | 2015-01-01 | 2015-01-02 | +---------------+------------+------------+ 1 row in set (0.01 sec) -- below call will insert only in usage table as row already present in sessions table mysql> call prc_insert_into_usage('job_b','2015-01-01','2015-01-02'); Query OK, 1 row affected (0.00 sec) mysql> select * from `usage`; +-------+------------+------------+---------------+ | job | start_time | end_time | session | +-------+------------+------------+---------------+ | job_a | 2015-01-01 | 2015-01-02 | sess_abc376bf | | job_b | 2015-01-01 | 2015-01-02 | sess_abc376bf | +-------+------------+------------+---------------+ 2 rows in set (0.00 sec) mysql> select * from `sessions`; +---------------+------------+------------+ | session_label | start_time | end_time | +---------------+------------+------------+ | sess_abc376bf | 2015-01-01 | 2015-01-02 | +---------------+------------+------------+ 1 row in set (0.00 sec) -- below call will again insert rows in both table mysql> call prc_insert_into_usage('job_c','2015-01-02','2015-01-04'); Query OK, 1 row affected (0.01 sec) mysql> select * from `usage`; +-------+------------+------------+---------------+ | job | start_time | end_time | session | +-------+------------+------------+---------------+ | job_a | 2015-01-01 | 2015-01-02 | sess_abc376bf | | job_b | 2015-01-01 | 2015-01-02 | sess_abc376bf | | job_c | 2015-01-02 | 2015-01-04 | sess_dcfa6853 | +-------+------------+------------+---------------+ 3 rows in set (0.00 sec) mysql> select * from `sessions`; +---------------+------------+------------+ | session_label | start_time | end_time | +---------------+------------+------------+ | sess_abc376bf | 2015-01-01 | 2015-01-02 | | sess_dcfa6853 | 2015-01-02 | 2015-01-04 | +---------------+------------+------------+ 2 rows in set (0.00 sec) mysql> - 首先检查文字“服务名称”,冒号和开头引号,然后捕获内部到下一个结束引号的内容。

但是,由于这看起来像是一个正确的JSON响应,因此您应该使用JSON序列化程序而不是RegEx。

编辑:根据@ neuronaut的评论,编辑出多余的"service_name":"([\w]+)",因为'\ w'已经覆盖了它。可以找到旧的RegEx101演示here,以获得相关性。

答案 1 :(得分:0)

我已经弄清楚了,要查看数组中的各个元素,我们可以使用以下内容: 在hive中的arrayName.ElementName [0]它将给出所有元素。要在画面中使用它,需要将其传递给直通函数,如:RAWSQL_STR(“%1.service_name [0]”,[服务详细信息])。