Question: I'm using an imagePicker that enables me to select 4 images and creates an array of the selected. I want to set the images of 4 empty UIImageView with those 4 images that I have selected. Using the images array: images.first, I can set the the first empty UIImageView to the first image I select. I can also set another empty UIImageView using images.last to get the fourth selected image. 

How do I set the image of the empty UIImageViews the second and third image I have selected?

I am using "if let" checks for the first and last element of the array, but I don't know if it's possible to do "if let" checks for the second and third elements in order to prevent my application from crashing if I do not select four images.

```
    func doneButtonDidPress(_ imagePicker: ImagePickerController, images: [UIImage]) {

       let images = imageAssets

       if let imageOne = images.first {

        profileImageViewOne.image = imageOne

       }

       profileImageViewTwo.image = images[1]
       profileImageViewThree.image = images[2]

       if let imageFour = images.last {

        profileImageViewFour.image = imageFour

       }       

    }

```

Answer: 
Understanding Lookahead
-----------------------

The reason is because you are misusing the lookahead windows. 

When using lookahead windows, it only looks ahead. **it does not capture** So in summary, you aren't actually capturing anything when using the `(?=[^a-z]*[a-z])(?=\D*\d)` those are only validating the conditions ahead. The capturing is done with the `\b`,`\w`,`\b`. So with that in mind, let's reinterpret your regex.

It says look for a string that starts with a word boundary, then **verifies** the word boundary is followed by these conditions before proceeding... starting from here we verify there will **possibly** be a few non-lowercase number, **will** be a lowercase number, **possibly** a few non numerical, **will** be a numerical.

Breakdown of Str1
-----------------

Take `str1="12AAayuA 12345"` and start at `12345`,

It starts with a word boundary, good. but the `?=[^a-z]*[a-z]` lookahead is not satisfied because it does not end with a character.

Breakdown of Str2
-----------------

Now take `str2="12AAayuA 12345 12AAayuA 12345"` and start at `12345 12AAayuA 12345`

It starts with a word boundary, good. Now look ahead, the `?=[^a-z]*[a-z]` is satisfied because `12345 12AA` are not lower-case characters but the following character after `12345 12AA` is a lowercase character. 

Now we move onto `(?=\D*\d)` But wait, lookahead window's look forward from the last expression which is **not** the lookback window but again it's the `\b`. So starting from `12345 12AAayuA 12345` we evaluate `(?=\D*\d)`, which is true 

Now that the look aheads are dealth with we can capture. So starting from `12345 12AAayuA 12345` we capture the rest `\w+\b` which is why you get `12345`

What is the correct solution?
-----------------------------

I'd go with 
`\b(?=\w*[a-z])(?=\w*\d)\w*\b`

* Finds beginning of word
* Checks if word has lowercase
* Checks if word has number
* Then captures/matches
This is a very complex regex that has few small flaws that impact the results in a big way. One flaw, as @Anthony mentions,

> 
> The character set [^a-z] in your look ahead assertion may be too
>  permissive, as it also includes the spaces between words.
> 
> 
> 

Let's take a step back and look at the requirements.

So you want a regex to validate a string with a lower case letter and a number.
Let's go over all of the possible ways that these 2 conditions can be met.
There are 2 formats where this can be true (**RA denotes random alphanumeric characters**):

* RA + lowercase + RA + number + RA `(\w*[a-z]+\w*\d+\w*)`
* RA + number + RA + lowercase + RA `(\w*\d+\w*[a-z]+\w*)`

Now we just have to combine these into one regex separated by a `|`. It's a little long, but it gets the job done.

`\b(\w*[a-z]+\w*\d+\w*)|(\w*\d+\w*[a-z]+\w*)\b`