I am looking for a regular expression that can get me src (case insensitive) tag from following HTML snippets in java.
<html><img src="kk.gif" alt="text"/></html>
<html><img src='kk.gif' alt="text"/></html>
<html><img src = "kk.gif" alt="text"/></html>
One possibility:
String imgRegex = "<img[^>]+src\\s*=\\s*['\"]([^'\"]+)['\"][^>]*>";
is a possibility (if matched case-insensitively). It's a bit of a mess, and deliberately ignores the case where quotes aren't used. To represent it without worrying about string escapes:
<img[^>]+src\s*=\s*['"]([^'"]+)['"][^>]*>
This matches:
<img
>
(i.e. possible other attributes)src
=
'
or "
>
(more possible attributes)>
to close the tagThings to note:
src=
as well, move the open bracket further left :-)>
or image sources that include '
or "
).If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With