正则表达式子串如果向后和向前斜杠“\ /”

时间:2022-05-31 21:18:56

What's the correct RegEx syntax for separating on a backward and forward slash?

用于分离后向和正向斜杠的正确RegEx语法是什么?

For Oracle I have tried:

对于Oracle,我尝试过:

string

word1\/word2\/word3

regexp_substr(string, '[^"\\/"]+', 1, 1) first

but it does not work correctly

但它无法正常工作

2 个解决方案

#1


You're using a character class [^"\\/"]; that selects all characters that aren't in the set {", /, \}. That's probably not what you want.

你正在使用一个字符类[^“\\ /”];选择不在集合{“,/,\}中的所有字符。这可能不是你想要的。

Instead, try

`.*?(\\/|$)`

This will match up to the \/ or the end of string, whichever comes first. Note that the \/ will be included in the match, because Oracle's regexp_substr doesn't appear to support lookahead assertions yet.

这将匹配字符串的\ /或结尾,以先到者为准。请注意,\ /将包含在匹配中,因为Oracle的regexp_substr似乎还不支持先行断言。

#2


If you want to get all the words you could just replace the \/ with space:

如果你想得到所有单词,你可以用空格替换\ /:

select REPLACE ('word1\/word2\/word3','\/',' ') "changes" FROM dual 

Or if you want only the first word:

或者如果你只想要第一个词:

select REGEXP_SUBSTR('word1\/word2\/word3', '[^\\^/]+') "changes" FROM dual

#1


You're using a character class [^"\\/"]; that selects all characters that aren't in the set {", /, \}. That's probably not what you want.

你正在使用一个字符类[^“\\ /”];选择不在集合{“,/,\}中的所有字符。这可能不是你想要的。

Instead, try

`.*?(\\/|$)`

This will match up to the \/ or the end of string, whichever comes first. Note that the \/ will be included in the match, because Oracle's regexp_substr doesn't appear to support lookahead assertions yet.

这将匹配字符串的\ /或结尾,以先到者为准。请注意,\ /将包含在匹配中,因为Oracle的regexp_substr似乎还不支持先行断言。

#2


If you want to get all the words you could just replace the \/ with space:

如果你想得到所有单词,你可以用空格替换\ /:

select REPLACE ('word1\/word2\/word3','\/',' ') "changes" FROM dual 

Or if you want only the first word:

或者如果你只想要第一个词:

select REGEXP_SUBSTR('word1\/word2\/word3', '[^\\^/]+') "changes" FROM dual