Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
145 views
in Technique[技术] by (71.8m points)

javascript - 从Java到MySQL的RegEx(RegEx from Javascript to MySQL)

I've this javascript regular expression that check if an URI is valid (RFC 3986):(我有这个JavaScript正则表达式,用于检查URI是否有效(RFC 3986):)

/^(https?)://((?:[a-z0-9.-]|%[0-9A-F]{2}){3,})(?::(d+))?((?:/(?:[a-z0-9-._~!$&'()*+,;=:@]|%[0-9A-F]{2})*)*)(?:?((?:[a-z0-9-._~!$&'()*+,;=:/?@]|%[0-9A-F]{2})*))?(?:#((?:[a-z0-9-._~!$&'()*+,;=:/?@]|%[0-9A-F]{2})*))?$/i Now i need to convert that in a MySQL query, using REGEXP .(现在,我需要使用REGEXP在MySQL查询中进行转换。) Eg:(例如:) SELECT * FROM table_name t WHERE t.uri REGEXP '....' Could you help me?(你可以帮帮我吗?)   ask by Alessio Pascucci translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You need to(你需要)

Double all ' chars inside '...' string literals(将'...'字符串文字内'...'所有'字符加倍) Replace all (?: with ( as MySQL legacy versions used a POSIX compliant regex engine that does not support non-capturing groups(将所有(?:替换为(因为MySQL旧版本使用的是POSIX兼容的正则表达式引擎,该引擎不支持非捕获组) Certainly remove the first / and last /i since the pattern is passed as a string in MySQL, not as a regex literal, and the pattern is case insensitive by default, no need to add i anywhere (or add AZ manually in case some global settings are overridden)(当然要删除第一个/和最后一个/i因为该模式是在MySQL中作为字符串而不是正则表达式文字传递的,并且该模式默认情况下不区分大小写,无需在任何地方添加i (或在某些全局情况下手动添加AZ设置被覆盖)) Replace all \/ with / just to keep the regex clean(全部替换\//只是为了保持干净的正则表达式) Replace \d with [0-9] (again, POSIX is not aware of shorthand character classes, although you may also use POSIX character classes, eg [[:digit:]] to match any digit)(用[0-9]替换\d (同样,POSIX不知道速记字符类,尽管您也可以使用POSIX字符类,例如[[:digit:]]来匹配任何数字)) Most likely, replace \?(最有可能取代\?) with \\?(与\\?) , or just use [?] to match a literal ?(,或仅使用[?]来匹配文字?) symbol(符号) Always use a literal hyphen at the end or start of a character class (bracket expression in POSIX regex).(始终在字符类的末尾或开头(POSIX正则表达式中的括号表达式)使用文字连字符。) Use(使用) WHERE t.uri REGEXP '^https?://(([A-Za-z0-9.-]|%[0-9A-Fa-f]{2}){3,})(:[0-9]+)?((/([A-Za-z0-9._~!$&''()*+,;=:@-]|%[0-9A-Fa-f]{2})*)*)([?](([A-Za-z0-9._~!$&''()*+,;=:/?@-]|%[0-9a-fA-F]{2})*))?(#(([A-Za-z0-9._~!$&''()*+,;=:/?@-]|%[0-9A-Fa-f]{2})*))?$'

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...