正则表达式

发布于 2024-10-26 02:55:29 字数 914 浏览 2 评论 0原文

我有两种方法来检查 11g 中的字符串中的特殊字符

1) 使用 SELECT

V_CNT_QRY := ' SELECT LENGTH(TRIM(TRANSLATE(:1,
                                             ''
                                             abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 '',
                                             '' '')))
                  from dual ';
EXECUTE IMMEDIATE  V_CNT_QRY INTO V_CNT USING V_COLUMN_DATA;

2) 使用过程

CREATE OR REPLACE PROCEDURE Validate_Inputstring(input_String IN VARCHAR2) AS
BEGIN
  IF REGEXP_LIKE(input_String, '^[A-Z0-9a-z]*$') THEN
    DBMS_OUTPUT.PUT_LINE('U have entered alphanumeric chars--->' ||
                         input_String);
  ELSE
    DBMS_OUTPUT.PUT_LINE('U NOT have entered alphanumeric chars---->' ||
                         input_String);
  END IF;
END;

请建议更好的方法

I have two ways to check a string for a special characters in 11g

1 ) Using SELECT

V_CNT_QRY := ' SELECT LENGTH(TRIM(TRANSLATE(:1,
                                             ''
                                             abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 '',
                                             '' '')))
                  from dual ';
EXECUTE IMMEDIATE  V_CNT_QRY INTO V_CNT USING V_COLUMN_DATA;

2) Using a procedure

CREATE OR REPLACE PROCEDURE Validate_Inputstring(input_String IN VARCHAR2) AS
BEGIN
  IF REGEXP_LIKE(input_String, '^[A-Z0-9a-z]*

Please suggest a better way

) THEN DBMS_OUTPUT.PUT_LINE('U have entered alphanumeric chars--->' || input_String); ELSE DBMS_OUTPUT.PUT_LINE('U NOT have entered alphanumeric chars---->' || input_String); END IF; END;

Please suggest a better way

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

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

发布评论

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

评论(3

月竹挽风 2024-11-02 02:55:29

假设你想检查字符串是否只包含 ASCII 字符,你可以使用:

Select 'No'
  From dual
 Where LENGTH(ASCIISTR(:input_String)) != LENGTH(:input_String)
 Union
Select 'Yes'
  From dual
 Where LENGTH(ASCIISTR(:input_String)) = LENGTH(:input_String)

HTH

Supposing you want to check if the string contains only ASCII characters, you can use this:

Select 'No'
  From dual
 Where LENGTH(ASCIISTR(:input_String)) != LENGTH(:input_String)
 Union
Select 'Yes'
  From dual
 Where LENGTH(ASCIISTR(:input_String)) = LENGTH(:input_String)

HTH

吲‖鸣 2024-11-02 02:55:29

我将按以下方式使用 regexp_replace 函数:

select 'You have'|| decode(regexp_replace (:txt,'^[A-Z0-9a-z]*

绑定变量 txt 包含要验证的字符串。

, '1'), '1', ' ', ' not ') || 'entered a string composed only by alphanum characters' from dual;

绑定变量 txt 包含要验证的字符串。

I would use the regexp_replace function in the following way:

select 'You have'|| decode(regexp_replace (:txt,'^[A-Z0-9a-z]*

The bind variable txt contains the string to validate.

, '1'), '1', ' ', ' not ') || 'entered a string composed only by alphanum characters' from dual;

The bind variable txt contains the string to validate.

回忆追雨的时光 2024-11-02 02:55:29
create table testx
( val1 varchar2(50));

insert into testx (val1) values ('Some ' || chr(9) || 'bad string');
insert into testx (val1) values ('Some nice string with 123 numbers');
commit;

select val1, regexp_instr(val1, '[^[:alnum:] ]') from testx;

如果字符串仅包含字母数字或空格,则 select 将返回 0,否则 > > 0.

因此,您可以添加一个 where 子句来仅获取“坏”字符串:

select val1 from testx where regexp_instr(val1, '[^[:alnum:] ]') > 0;

当然,您可以根据需要根据您对“特殊字符”的定义进行修改。

编辑:
如果您不关心标记标点符号,请尝试:

select val1 from testx where regexp_instr(val1, '[^[:alnum:] [:punct:]]') > 0;

再次根据您的定义进行修改

create table testx
( val1 varchar2(50));

insert into testx (val1) values ('Some ' || chr(9) || 'bad string');
insert into testx (val1) values ('Some nice string with 123 numbers');
commit;

select val1, regexp_instr(val1, '[^[:alnum:] ]') from testx;

The select will return 0 if string contains only alphanumeric or spaces, else > 0.

So, you can add a where clause to grab just the "bad" strings:

select val1 from testx where regexp_instr(val1, '[^[:alnum:] ]') > 0;

Of course you can modify this as needed depending on your definition of "special characters".

EDIT:
if you don't care about flagging punctuation, try:

select val1 from testx where regexp_instr(val1, '[^[:alnum:] [:punct:]]') > 0;

again, modify as needed to your definition

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