REGEXP以“字母”结尾,该字母后面可能有空格

时间:2018-07-31 16:49:25

标签: sql regex database

我有REGEXP表达式,该表达式需要接受特定字母的开头,介于特定结尾字母之间的任何内容,并且在该结尾字母之后可能还会有空格。 (来自数据库) 当我运行表达式时,它不会给我结尾字母,因为它在我正在搜索的名称后面的数据库中有空格

 WHERE REGEXP_LIKE (cname, UPPER('^[&p_name_beginning](.*?)[&p_name_ending$]'));

输出:

JIE DONG has bought 2 car(s) and has spent $151200
JAMES BARREDO has bought 1 car(s) and has spent $300145
JUAN MENDIOLA has bought 1 car(s) and has spent $75610.89
JASON HADDAD has bought 1 car(s) and has spent $157000
JOSE ANDRADE has bought 1 car(s) and has spent $151046
JORDAN PENNEY has bought 1 car(s) and has spent $85201.92
JUAN RODAS has bought 1 car(s) and has spent $105000

1 个答案:

答案 0 :(得分:0)

如果您在数据前后指定要对样本进行的操作以及显示所尝试的内容,将会获得更好的帮助。我怀疑您正在尝试选择名称的首字母和尾字母与您所给定的参数匹配的行。如果您更新标签以显示正在使用的数据库,则会得到更有针对性的答案,但是我这里提供了一种Oracle解决方案,可以返回第3条记录,如果我的假设正确,则该记录会有所帮助。它会给您任何提示。

with tbl(str) as (
  select 'JIE DONG has bought 2 car(s) and has spent $151200' from dual union all
  select 'JAMES BARREDO has bought 1 car(s) and has spent $300145' from dual union all
  select 'JUAN MENDIOLA has bought 1 car(s) and has spent $75610.89' from dual union all
  select 'JASON HADDAD has bought 1 car(s) and has spent $157000' from dual union all
  select 'JOSE ANDRADE has bought 1 car(s) and has spent $151046' from dual union all
  select 'JORDAN PENNEY has bought 1 car(s) and has spent $85201.92' from dual union all
  select 'JUAN RODAS has bought 1 car(s) and has spent $105000' from dual
)
select str
from tbl
where regexp_like(str, '^j\S+ \S+a .*$', 'i');

正则表达式如下:

^       Anchor to the start of the line
j       Match a 'j' (first letter of name)
\S+     Followed by one or more characters that are not spaces
<space> Then a space character
\S+     Followed by one or more characters that are not spaces
a       Then the ending letter of the name
<space> Followed by a space character
.*      Followed by zero or more of any characters
$       The end of the line

“ i”表示不区分大小写。