目前正则表达式仅验证以.jpg .jpeg .png和.gif结尾的简单HTML图片链接
是否有一种简单的方法可以修改以下正则表达式以接受i.imgur.com/____.extension等链接或多个句点分隔的链接,例如来自维基百科等网站/论坛的图片。
例如,如何更改当前正则表达式以接受此类链接? https://upload.wikimedia.org/wikipedia/commons/2/23/The_river_effect_in_justified_text.jpg
当前的正则表达式是:
([a-z\-_0-9\/\:\.]*\.(jp$g|png|gif)$)
我知道我可以用它来检查匹配[]内任何字符的字符。
[a-z\-_0-9\/\:\.]*\.
以下是我正在尝试接受图像的正则表达式的段。以前这是
的正则表达式custonValidation:customRegexp =" ([A-Z-_0-9 / :.] *(JP $克| PNG | GIF)$。)"
<com.andreabaccega.widget.FormEditText
customValidation:testType="regexp"
customValidation:customRegexp="^((?!mailto:)(?:(?:https?|ftp)://)?(?:\S+(?::\S*)?@)?(?:(?:(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\u00a1-\uffff0-9]+-?)*[a-z\u00a1-\uffff0-9]+)(?:\.(?:[a-z\u00a1-\uffff0-9]+-?)*[a-z\u00a1-\uffff0-9]+)*(?:\.(?:[a-z\u00a1-\uffff]{2,})))|localhost))(?::\d{2,5})?(?:/(?:[^\s/]*/)*)([^/]+\.(?:jpe?g|png|gif))$"
customValidation:emptyAllowed="true"
customValidation:testErrorString="This is not a valid URL"
android:id="@+id/edit_imgpath_entry"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:inputType="textMultiLine"
android:gravity="top"
android:minLines="1"
android:layout_weight="1" />
我理解正则表达式的大多数基础知识但我不确定如何扩展它以接受更复杂的情况。
感谢任何帮助!
答案 0 :(得分:0)
您需要在图像之前进行某种类型的URL验证。
我猜你可以用这个
做到这一点 \ x {}表示法:^((?!mailto:)(?:(?:https?|ftp)://)?(?:\S+(?::\S*)?@)?(?:(?:(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\x{a1}-\x{ffff}0-9]+-?)*[a-z\x{a1}-\x{ffff}0-9]+)(?:\.(?:[a-z\x{a1}-\x{ffff}0-9]+-?)*[a-z\x{a1}-\x{ffff}0-9]+)*(?:\.(?:[a-z\x{a1}-\x{ffff}]{2,})))|localhost))(?::\d{2,5})?(?:/(?:[^\s/]*/)*)([^/]+\.(?:jpe?g|png|gif))$
“”弦乐:"^((?!mailto:)(?:(?:https?|ftp)://)?(?:\\S+(?::\\S*)?@)?(?:(?:(?:[1-9]\\d?|1\\d\\d|2[01]\\d|22[0-3])(?:\\.(?:1?\\d{1,2}|2[0-4]\\d|25[0-5])){2}(?:\\.(?:[1-9]\\d?|1\\d\\d|2[0-4]\\d|25[0-4]))|(?:(?:[a-z\\x{a1}-\\x{ffff}0-9]+-?)*[a-z\\x{a1}-\\x{ffff}0-9]+)(?:\\.(?:[a-z\\x{a1}-\\x{ffff}0-9]+-?)*[a-z\\x{a1}-\\x{ffff}0-9]+)*(?:\\.(?:[a-z\\x{a1}-\\x{ffff}]{2,})))|localhost))(?::\\d{2,5})?(?:/(?:[^\\s/]*/)*)([^/]+\\.(?:jpe?g|png|gif))$"
\ u表示法:^((?!mailto:)(?:(?:https?|ftp)://)?(?:\S+(?::\S*)?@)?(?:(?:(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\u00a1-\uffff0-9]+-?)*[a-z\u00a1-\uffff0-9]+)(?:\.(?:[a-z\u00a1-\uffff0-9]+-?)*[a-z\u00a1-\uffff0-9]+)*(?:\.(?:[a-z\u00a1-\uffff]{2,})))|localhost))(?::\d{2,5})?(?:/(?:[^\s/]*/)*)([^/]+\.(?:jpe?g|png|gif))$
“”弦乐:"^((?!mailto:)(?:(?:https?|ftp)://)?(?:\\S+(?::\\S*)?@)?(?:(?:(?:[1-9]\\d?|1\\d\\d|2[01]\\d|22[0-3])(?:\\.(?:1?\\d{1,2}|2[0-4]\\d|25[0-5])){2}(?:\\.(?:[1-9]\\d?|1\\d\\d|2[0-4]\\d|25[0-4]))|(?:(?:[a-z\\u00a1-\\uffff0-9]+-?)*[a-z\\u00a1-\\uffff0-9]+)(?:\\.(?:[a-z\\u00a1-\\uffff0-9]+-?)*[a-z\\u00a1-\\uffff0-9]+)*(?:\\.(?:[a-z\\u00a1-\\uffff]{2,})))|localhost))(?::\\d{2,5})?(?:/(?:[^\\s/]*/)*)([^/]+\\.(?:jpe?g|png|gif))$"
格式化 - 可读版本:
^
( # (1 start), Domain
(?! mailto: )
(?:
(?: https? | ftp )
://
)?
(?:
\S+
(?: : \S* )?
@
)?
(?:
(?:
(?:
[1-9] \d?
| 1 \d\d
| 2 [01] \d
| 22 [0-3]
)
(?:
\.
(?: 1? \d{1,2} | 2 [0-4] \d | 25 [0-5] )
){2}
(?:
\.
(?:
[1-9] \d?
| 1 \d\d
| 2 [0-4] \d
| 25 [0-4]
)
)
| (?:
(?: [a-z\x{a1}-\x{ffff}0-9]+ -? )*
[a-z\x{a1}-\x{ffff}0-9]+
)
(?:
\.
(?: [a-z\x{a1}-\x{ffff}0-9]+ -? )*
[a-z\x{a1}-\x{ffff}0-9]+
)*
(?:
\.
(?: [a-z\x{a1}-\x{ffff}]{2,} )
)
)
| localhost
)
) # (1 end)
(?: : \d{2,5} )?
(?: # Directory's
/
(?: [^\s/]* / )*
)
( # (2 start), The image
[^/]+ \.
(?: jpe?g | png | gif )
) # (2 end)
$
示例输出:
** Grp 0 - ( pos 0 : len 90 )
https://upload.wikimedia.org/wikipedia/commons/2/23/The_river_effect_in_justified_text.jpg
** Grp 1 - ( pos 0 : len 28 )
https://upload.wikimedia.org
** Grp 2 - ( pos 52 : len 38 )
The_river_effect_in_justified_text.jpg