正则表达式,行尾或其他字符而不是" - "

时间:2015-04-21 15:34:44

标签: regex oracle

我在这里制作了一个正则表达式以显示我的问题: https://regex101.com/r/zV2oL1/1

前两行是正常的,但是当该行在末尾附近有一个“ - ”时,它不应该匹配,就像在这种情况下:第三行。

WILD CARROT - POTATOE 1 PIE 2
WILD CARROT - POTATOE 1 PIE 2 OK
WILD CARROT - POTATOE 1 PIE 2 - NOT OK

我不明白为什么要结束我的正则表达式:

($|[^-])

不起作用。

更新要清楚,只有前两行应匹配

4 个答案:

答案 0 :(得分:1)

尝试类似:

IF REGEXP_LIKE(subject, '.*?POTATOE.*?\d.*?PIE.*?\d', 'im') THEN
    -- Successful match
ELSE
    -- Match attempt failed
END IF;

说明:

.*?POTATOE.*?\d.*?PIE.*?\d

Options: Case insensitive; Exact spacing; Dot doesn't match line breaks; ^$ match at line breaks

Match any single character that is NOT a line break character (line feed) «.*?»
   Between zero and unlimited times, as few times as possible, expanding as needed (lazy) «*?»
Match the character string “POTATOE” literally (case insensitive) «POTATOE»
Match any single character that is NOT a line break character (line feed) «.*?»
   Between zero and unlimited times, as few times as possible, expanding as needed (lazy) «*?»
Match a single character that is a “digit” (any decimal number in any Unicode script) «\d»
Match any single character that is NOT a line break character (line feed) «.*?»
   Between zero and unlimited times, as few times as possible, expanding as needed (lazy) «*?»
Match the character string “PIE” literally (case insensitive) «PIE»
Match any single character that is NOT a line break character (line feed) «.*?»
   Between zero and unlimited times, as few times as possible, expanding as needed (lazy) «*?»
Match a single character that is a “digit” (any decimal number in any Unicode script) «\d»

答案 1 :(得分:1)

你的正则表达式正在检查行结尾$或字符串中不是-的字符(因为坏字符串的结尾是匹配的)行和不是-的字符。要匹配任何地方没有-的字符串,请使用:
^[^-]*$
要匹配行尾没有字符-的字符串,请使用:
[^-]$
我希望这有帮助!

答案 2 :(得分:1)

也许是这样的

.*POTATOE.*?\d.*?PIE.*?\d[^\-]*$

答案 3 :(得分:0)

您可以尝试:

^.*POTATOE.*?\d.*?PIE \d[^-]*$

将“m”和“g”修饰符放在一起。导致:

/^.*POTATOE.*?\d.*?PIE \d[^-]*$/mg