从给定模式中提取双引号字符串

发布于 2025-01-06 14:16:14 字数 382 浏览 0 评论 0原文

请任何人帮助我,

我有一个像 varchar2 b 一样的字符串

'i hav to extract second double queted string "string one".and the "Second one"'

预期结果:第二个

varchar2 a:

' here is "table". "tiger" some other txt ';

预期结果是 tiger

从上面的字符串模式中我必须提取第二个双引号字符串精度。请在这方面帮助我,我已经尝试了很多尝试

Please any one help me,

I have a string like

varchar2 b :

'i hav to extract second double queted string "string one".and the "Second one"'

Expected result : Second One

varchar2 a :

' here is "table". "tiger" some other txt ';

expected result is tiger

from the above string patterns i have to extract the second double quoted string accurence. Please help me in this regard i have tried many attempts

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

霞映澄塘 2025-01-13 14:16:14

在 11g 中,您可以使用 regexp_substr 带有新参数(允许仅匹配子表达式):

SQL> with data as (
  2    select 'i hav to [...] "string one".and the "Second one"' txt from dual
  3    union all
  4    select ' here is "table". "tiger" some other txt ' from dual)
  5  SELECT regexp_substr(txt,'"([^"]*)"', 1, 2, '', 1) FROM data;

REGEXP_SUBSTR(TXT,'"([^"]*)"',1,2,'',1)
------------------------------------------------------------------------------
Second one
tiger

在 10g 中,您可以使用replace 删除多余的 "

SQL> with data as (
  2    select 'i hav to [...] "string one".and the "Second one"' txt from dual
  3    union all
  4    select ' here is "table". "tiger" some other txt ' from dual)
  5  SELECT replace(regexp_substr(txt,'"[^"]*"', 1, 2),
  6                 '"', '')
  7    FROM data;

REPLACE(REGEXP_SUBSTR(TXT,'"[^"]*"',1,2),'"','')
------------------------------------------------
Second one
tiger

In 11g, you can use regexp_substr with the new argument (that allows to match only a subexpression):

SQL> with data as (
  2    select 'i hav to [...] "string one".and the "Second one"' txt from dual
  3    union all
  4    select ' here is "table". "tiger" some other txt ' from dual)
  5  SELECT regexp_substr(txt,'"([^"]*)"', 1, 2, '', 1) FROM data;

REGEXP_SUBSTR(TXT,'"([^"]*)"',1,2,'',1)
------------------------------------------------------------------------------
Second one
tiger

In 10g, you could use replace to remove the extra ":

SQL> with data as (
  2    select 'i hav to [...] "string one".and the "Second one"' txt from dual
  3    union all
  4    select ' here is "table". "tiger" some other txt ' from dual)
  5  SELECT replace(regexp_substr(txt,'"[^"]*"', 1, 2),
  6                 '"', '')
  7    FROM data;

REPLACE(REGEXP_SUBSTR(TXT,'"[^"]*"',1,2),'"','')
------------------------------------------------
Second one
tiger
指尖上的星空 2025-01-13 14:16:14

您可以使用REGEXP_REPLACE

WITH t AS (
   SELECT 'i hav to extract second double queted string "string one".and the "Second one"'  as x FROM dual
   UNION
   SELECT ' here is "table". "tiger" some other txt ' as x FROM dual)
SELECT x,
       REGEXP_REPLACE(x, '^.*".*".*(".*").*

返回:

"tiger"
"Second one"

希望有帮助。 ..

如果您不需要引号,请使用:

REGEXP_REPLACE(x, '^.*".*".*"(.*)".*
, '\1')
  FROM t;

返回:


希望有帮助。 ..

如果您不需要引号,请使用:


, '\1')
, '\1') FROM t;

返回:

希望有帮助。 ..

如果您不需要引号,请使用:

You could use REGEXP_REPLACE:

WITH t AS (
   SELECT 'i hav to extract second double queted string "string one".and the "Second one"'  as x FROM dual
   UNION
   SELECT ' here is "table". "tiger" some other txt ' as x FROM dual)
SELECT x,
       REGEXP_REPLACE(x, '^.*".*".*(".*").*

Returns:

"tiger"
"Second one"

Hope it helps...

If you don't want the quotes then use:

REGEXP_REPLACE(x, '^.*".*".*"(.*)".*
, '\1')
  FROM t;

Returns:


Hope it helps...

If you don't want the quotes then use:


, '\1')
, '\1') FROM t;

Returns:

Hope it helps...

If you don't want the quotes then use:

剩一世无双 2025-01-13 14:16:14

使用 instr() 获取字符索引(以及要获取的字符)和 substr() 获取字符串的子字符串的示例:

select 
    substr(str,
        instr(str, '"', 1,3)+1, 
        instr(str, '"', 1, 4)- instr(str, '"', 1,3)-1)
from
   (select 'here is "table". "tiger" some other txt' str from dual) strt;

这里 substr 使用 instr(str, '"',1,3) 获取第三次出现的 '"'。然后,它使用 instr(str, '"', 1, 4) 来获取第四次出现,但我们必须减去第三个 '"' 的位置,因为此参数是文本的大小子字符串(即本例中引号之间的文本)。

您可以改进我们获取第四次出现的方式,因为它再次从位置 1 而不是位置 3 开始搜索。

An example using instr() to get index of a character (and which occurence to get) and substr() to take a substring of a string:

select 
    substr(str,
        instr(str, '"', 1,3)+1, 
        instr(str, '"', 1, 4)- instr(str, '"', 1,3)-1)
from
   (select 'here is "table". "tiger" some other txt' str from dual) strt;

Here substr uses instr(str, '"',1,3) to get the third occurence of '"'. It then uses instr(str, '"', 1, 4) to get the fourth occurence but we have to substract the position of the third '"' as this parameter is the size of the text to substring (i.e. the text between the quotes in our case).

You can improve on how we get the fourth occurence as it starts searching from position 1 again, rather than position 3.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文