PeakGen
PeakGen

Reputation: 22995

Regex: How to operate "AND" kind of expression in Regex?

I need to get the URL from the below 2 snippets

  1. <img src='https://aaa.s3.amazonaws.com/1523622623-aaaa.jpg' width='60' align='left' hspace='5'/>

  2. <img src="https://aaa.s3.amazonaws.com/1523622623-aaaa.jpg" width='60' align='left' hspace='5'/>

The snipets may look similar, but see how the URL is covered with single quote (') in the first URL and with double quote (") in the second URL. Below is my code, to extract the URL from double quote.

//Tryig to find images in descriptions
 String str = property.getFirstChild().getNodeValue();
 Pattern p = Pattern.compile("src=\"(.*?)\"");
 Matcher m = p.matcher(str);
 if (m.find()) {
    newsDataBean.setNewsImageUrl(m.group(1));
    Log.d("FeedParser", "DESCRIPTION_IMAGE: " + m.group(1));
 }

I can get the URL in the 1st snippet, if I change my REGEX to ("src=\'(.*?)\'"). I want the regex to extract URL from either double quote or single quote. How can I do that?

Upvotes: 0

Views: 40

Answers (1)

gil.fernandes
gil.fernandes

Reputation: 14591

You can try OR clauses in order to extract with either double or single quotes:

Pattern p = Pattern.compile("src=('|\")(.*?)('|\")");

or character classes:

Pattern p = Pattern.compile("src=['\"](.*?)['\"]");

The latter is the better option.

Alternatively you can also use a back-reference so that single quote matches single quote or double quote matches double quote:

Pattern p = Pattern.compile("src=(['\"])(.*?)\\1");

Upvotes: 3

Related Questions