[英]ORacle SQL Regular Expressions
I'm looking to create some regular expressions code for some testing. 我想为一些测试创建一些正则表达式代码。 In the below I have created a Constraint Check for an email address.
在下面,我创建了一个电子邮件地址的约束检查。
create table testemail (username varchar2(50) NOT NULL,
Password varchar(15) Not NULL,
CONSTRAINT pk_usertest PRIMARY KEY (username),
CONSTRAINT un_emailtest CHECK (REGEXP_LIKE(username,'^([[:alnum:]]+)@[[:alnum:]]+.(com|net|org|edu|gov|mil)$'))
Is there a better way to have a constraint check when a new user creating an account? 在新用户创建帐户时,是否有更好的方法进行约束检查?
Also, I'm trying to search a user by the name of 'Luke Haire' but my below sql queries returns no results: 此外,我正在尝试以“Luke Haire”的名义搜索用户,但我的下面的SQL查询没有返回任何结果:
select * from customers where regexp_like (name, '^([L(u|i|o)ke]+)[\][Haire]$');
I don't really understand your first question. 我真的不明白你的第一个问题。 A check constraint is the best way to have a check constraints.
检查约束是检查约束的最佳方法。 That is why databases support it.
这就是数据库支持它的原因。 There are worse ways, such as triggers, but typically the best way is using this built-in, standard capability.
有更糟糕的方法,例如触发器,但通常最好的方法是使用这种内置的标准功能。
Your specific constraint seems too constrained. 您的具体约束似乎过于受限制。 Email addresses commonly contain "."
电子邮件地址通常包含“。” and "-" as well.
和“ - ”。
As for the second question, the problem is the suffix. 至于第二个问题,问题是后缀。 So try:
所以尝试:
select *
from customers
where regexp_like(name, '^([L(u|i|o)ke]+)[\][Haire]@.*$');
---------------------------------------------------^
I should add that I prefer the above to the equivalent: 我应该补充一点,我更喜欢上面的等价物:
where regexp_like(name, '^([L(u|i|o)ke]+)[\][Haire]@');
The issue is the difference between like
and regexp
. 问题是
like
和regexp
之间的区别。 like
always matches the entire string. like
总是匹配整个字符串。 So, under most circumstances, I prefer to have regular expressions emulate this by explicitly having the beginning and end. 所以,在大多数情况下,我更喜欢让正则表达式通过显式开头和结尾来模拟它。 This is to avoid confusion.
这是为了避免混淆。 I use like for simpler searches and regular expressions for more complex ones.
我使用更简单的搜索和更复杂的正则表达式。
However, I would still expect this to get no matches because \\
is not an allowed character before the @
, according to the check constraint. 但是,根据检查约束,我仍然期望这不会得到匹配,因为
\\
在@
之前不是允许的字符。
Your query doesn't return any rows because you have two mistakes in your pattern. 您的查询不会返回任何行,因为您的模式中有两个错误。
Second expected character, according to your pattern is \\ so in character list [\\]
you should add space [\\ ]
or [\\[:space:]]
. 第二个预期字符,根据你的模式是\\所以在字符列表
[\\]
你应该添加空格[\\ ]
或[\\[:space:]]
。 You are missing quantifier for [Haire] character list. 您缺少[Haire]角色列表的量词。 At the moment your pattern works like:
目前您的模式如下:
regexp_like ('Luke Haire', '^([L(u|i|o)ke]+)[\](H|a|i|r|e)$')
One possible pattern for your entry string is: 您的输入字符串的一种可能模式是:
regexp_like (name, '^([L(u|i|o)ke]+)[\ ][Haire].*$')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.