需要一条正则绳索来使字符串由下划线
分开我有字符串one_two_three_four
我需要一条正则是从上面的字符串
获得第二个字符串two
添加了更多信息:
我想要此正则是RDBMS表,其中列中的数据将以" One_two_three_four"的格式使用,所以我只想从列中从列中获取"两个"。使用以下查询。相反,我再次在Java中获取数据并拆分数据。
select distinct(REGEXP_SUBSTR(column_name, 'regex'))
from table_name
在Oracle中您确实可以使用Regexp_substr。
或与INSER结合使用的正常子源以获得下划线的位置。
例如:
select
-- getting the 2nd match with non-underscore characters
regexp_substr(val, '[^_]+', 1, 2) as method1,
-- getting the (capture group)
regexp_substr(val, '^.*?_(.*?)_', 1, 1, null, 1) as method2,
-- substring on the positions of the underscores
substr(val,instr(val,'_')+1,instr(val,'_',1,2)-instr(val,'_')-1) as method3
from (
select 'one_two_three_four' as val from dual
) q
和像Java这样的编程语言?
为什么何时可以分裂呢?
String str = "one_two_three_four";
将字符串分为数组,然后将元素1
分开String[] strArray = str.split("_");
String secondSplit = strArray[1];
或一枪:
String secondSplit = str.split("_")[1];
顺便说一句,拆分还采用正则表达式。因此,这也是如此:
String secondSplit = str.split("[_]")[1];