regular expressions est way is to use the regex \ (helen\ +patricia)\ +sharman\ but we could also achieve the same thing using lookahead assertionfor example\ (helen\ +patricia)(?=\ +sharman\bthis will match "helen patriciaonly if it is preceded by word boundary and followed by whitespace and "sharmanending at word boundary to capture the particular variation of the forenames that is used ("helen""helen "or "helen patricia")we could make the regex slightly more sophisticatedfor example\ (helen(?:\ +(?: |patricia))?)\ +(?=sharman\bthis matches word boundary followed by one of the forename forms--but only if this is followed by some whitespace and then "sharmanand word boundary note that only two syntaxes perform capturing(eand (?penone of the other parenthesized forms captures this makes perfect sense for the lookahead and lookbehind assertions since they only make statement about what follows or precedes them--they are not part of the matchbut rather affect whether match is made it also makes sense for the last two parenthesized forms that we will now consider we saw earlier how we can backreference capture inside regex either by number ( \ or by name ( (? =name)it is also possible to match conditionally depending on whether an earlier match occurred the syntaxes are (?(id)yes_expand (?(id)yes_exp|no_expthe id is the name or number of an earlier capture that we are referring to if the capture succeeded the yes_exp will be matched here if the capture failed the no_exp will be matched if it is given let' consider an example suppose we want to extract the filenames referred to by the src attribute in html img tags we will begin just by trying to match the src attributebut unlike our earlier attempt we will account for the three forms that the attribute' value can takesingle quoteddouble quotedand unquoted here is an initial attemptsrc=(["'])([^"'>]+)\ the ([^"'>]+part captures greedy match of at least one character that isn' quote or this regex works fine for quoted filenamesand thanks to the \ matches only when the opening and closing quotes are the same but it does not allow for unquoted filenames to fix this we must make the opening quote optional and therefore match only if it is present here is revised regexsrc=(["'])?([^"'>]+)(?( )\ we did not provide no_exp since there is nothing to match if no quote is given unfortunatelythis doesn' work quite right it will work fine for quoted filenamesbut for unquoted filenames it will work only if the src attribute is the last attribute in the tagotherwise it will incorrectly match text into the next attribute the solution is to treat the two cases (quoted and unquotedseparatelyand to use alternationsrc=((["'])([^\ >]+?)\ |([^">]+)now let' see the regex in contextcomplete with named groupsnonmatching parenthesesand comments