简体   繁体   English

使用oracle REGEXP_INSTR的正则表达式

[英]regular expression using oracle REGEXP_INSTR

I want to use REGEXP_INSTR function in a query to search for any match for user input but I don't know how to write the regular expression that for example will match any value that includes the word car followed by unspecified numbers of letters/numbers/spaces and then the word Paterson. 我想在查询中使用REGEXP_INSTR函数来搜索用户输入的任何匹配项,但是我不知道如何编写正则表达式,例如将匹配包含单词car的任何值,后跟未指定数字的字母/数字/空格,然后是Paterson一词。 can any one please help me with writing this regEx? 有人可以帮我写这篇正则表达式吗?

Ok, so let's break this down. 好的,让我们分解一下。

"any value that includes the word car" “任何包含汽车一词的价值”

I surmise from this that the word car doesn't need to be at the start of the string, therefore i would start the format string with... 我由此推测,单词car不需要在字符串的开头,因此我将以...开头格式字符串。

'^.*'

Here the '^' character means the start of the string, the '.' 此处的“ ^”字符表示字符串的开头,即“。”。 means any character and '*' means 0 or more of the preceding character. 表示任何字符,“ *”表示0个或多个前面的字符。 So zero or more of any character after the start of the string. 因此,在字符串开头之后,任何字符的零个或多个。

Then the word 'car', so... 然后是“汽车”一词,所以...

'^.*car'

Next up... 接下来...

"followed by unspecified numbers of letters/numbers/spaces" “之后是未指定的字母/数字/空格”

I'm guessing that unspecified means zero or more. 我猜未指定意味着零或更多。 This is very similar to what we did to identify any characters that might come before 'car'. 这与我们识别“汽车”之前可能出现的任何字符非常相似。 Where the '.' 哪里的“。” means any character 表示任何字符

'^.*car.*'

However, if unspecified means one or more, then you can use '+' in place of '*' 但是,如果未指定表示一个或多个,则可以使用“ +”代替“ *”

"then the word Paterson" “然后这个词帕特森”

I'm going to assume that as this is the end of the description, there are no more characters after 'Paterson'. 我将假设由于这是描述的结尾,因此在“ Paterson”之后不再有其他字符。

'^.*car.*Paterson$'

The '$' symbol means that the 'n' of 'Paterson' must be at the end of the string. “ $”符号表示“ Paterson”的“ n”必须在字符串的末尾。

Code example: 代码示例:

select
    REGEXP_INSTR('123456car1234ABCDPaterson', '^.*car.*Paterson$') as rgx
from dual

Output 输出量

       RGX
----------
         1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM