How can I make part of regex optional?

To make the .+ optional, you could do:

\"(?:.+)?\";

(?:..) is called a non-capturing group. It only does the matching operation and it won’t capture anything. Adding ? after the non-capturing group makes the whole non-capturing group optional.

Alternatively, you could do:

\".*?\";

.* would match any character zero or more times greedily. Adding ? after the * forces the regex engine to do a shortest possible match.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)