oracle 如何用·替换单词,但只显示de的第一个和最后一个字符

snvhrwxg  于 2023-05-28  发布在  Oracle
关注(0)|答案(3)|浏览(124)

在甲骨文中,用符号代替一个单词,但要保留第一个和最后一个字符.
我试过REGEXP_REPLACE('ANNE MARRY', '(\w)\w', '\1*')
我想当我介绍“安妮玛丽”我的查询返回“A··e M···y”
这是REGEXP_REPLACE的解决方案?

ddrv8njm

ddrv8njm1#

Oracle不支持使用单个正则表达式解决此问题所需的语法。
您可以使用简单的字符串函数和递归子查询,而无需正则表达式:

WITH replacements (id, value, epos) AS (
  SELECT ROWNUM, value, 1 FROM table_name
UNION ALL
  SELECT id,
         CASE
         WHEN INSTR(value, ' ', epos) = 0 AND epos <= LENGTH(value) - 2
         THEN SUBSTR(value, 1, epos)
              || LPAD('•', LENGTH(value) - epos - 1, '•')
              || SUBSTR(value, -1)
         WHEN INSTR(value, ' ', epos) <= epos + 2
         THEN value
         ELSE SUBSTR(value, 1, epos)
              || LPAD('•', INSTR(value, ' ', epos) - epos - 2, '•')
              || SUBSTR(value, INSTR(value, ' ', epos) - 1)
         END,
         INSTR(value, ' ', epos) + 1
  FROM   replacements
)
SEARCH DEPTH FIRST BY id SET order_id
CYCLE id, epos SET is_cycle TO 1 DEFAULT 0
SELECT value
FROM   replacements
WHERE  is_cycle = 1;

其中,对于样本数据:

CREATE TABLE table_name (value) AS
  SELECT 'ANNE MARRY' FROM DUAL UNION ALL
  SELECT 'XI NG' FROM DUAL UNION ALL
  SELECT 'A B C D E' FROM DUAL UNION ALL
  SELECT 'MAY' FROM DUAL;

输出:
| 价值|
| - -----|
| A··E M···Y|
| 吴锡|
| A B C D E|
| M·Y|
fiddle

z0qdvdin

z0qdvdin2#

我不知道在Oracle中使用的是哪种正则表达式引擎,但是如果你可以使用负向后看和向前看,那么你就可以匹配任何前面或后面没有单词边界的单词字符。在JavaScript中,你可以使用这个模式:

/(?<!\b)\w(?!\b)/g

你可以在这里测试:https://regex101.com/r/N7yJDP/1
或者在这里运行:

const regex = /(?<!\b)\w(?!\b)/g;
const input = `Annie Marry is probably a cook`;
const substitution = `•`;

const result = input.replace(regex, substitution);

console.log('result: ', result);
qhhrdooz

qhhrdooz3#

我做了类似的事情,使用一个视图来显示屏蔽的数据和一个触发器
下面是我的步骤。

CREATE TABLE CARDS_TBL (
  CARD_ID NUMBER
    GENERATED BY DEFAULT AS IDENTITY,
  CARD_STR VARCHAR2(16) NOT NULL,
  PRIMARY KEY (CARD_ID)
);

INSERT INTO CARDS_TBL(CARD_STR) VALUES('4024007187788590');
INSERT INTO CARDS_TBL(CARD_STR) VALUES('5432223398564536');
INSERT INTO CARDS_TBL(CARD_STR) VALUES('5430445512530934');
INSERT INTO CARDS_TBL(CARD_STR) VALUES('4020156755227854');
INSERT INTO CARDS_TBL(CARD_STR) VALUES('5431248766892318');

CREATE OR REPLACE VIEW CARDS AS
  SELECT
    CARD_ID,
    REGEXP_REPLACE(CARD_STR, 
'(\w)(\w*(\w))?', '\1**********\3') AS CARD_STR
  FROM CARDS_TBL;

CREATE OR REPLACE TRIGGER CARDS_TBL_TRG_INSERT
  INSTEAD OF INSERT ON CARDS 
  FOR EACH ROW
BEGIN
  INSERT INTO CARDS_TBL (CARD_STR) VALUES (:NEW.CARD_STR);
END;
/

INSERT INTO CARDS_TBL (CARD_STR) VALUES ('2222333344445555');

SELECT * FROM CARDS_TBL;

CARD_ID CARD_STR
1   4024007187788590
2   5432223398564536
3   5430445512530934
4   4020156755227854
5   5431248766892318
21  2222333344445555

UPDATE CARDS_TBL 
  SET CARD_STR = '2222333344445566'
  WHERE CARD_ID = 5;
/

SELECT * FROM CARDS;

CARD_ID CARD_STR
1   4**********0
2   5**********6
3   5**********4
4   4**********4
5   2**********6
21  2**********5

SELECT * FROM CARDS_TBL;

CARD_ID CARD_STR
1   4024007187788590
2   5432223398564536
3   5430445512530934
4   4020156755227854
5   2222333344445566
21  2222333344445555

相关问题