EN VI

How to form a regex for a double quote that may or may not exist (Flutter)?

2024-03-12 13:30:05
How to form a regex for a double quote that may or may not exist (Flutter)

It's possible that I will either have

boundary=-------Embt-Boundary--1261032946D275CF

or

boundary="-------Embt-Boundary--1261032946D275CF"

and I want to just extract -------Embt-Boundary--1261032946D275CF from the String.

How should I formed the regex? I have tried boundary="?(.+)("?) but it only works for the first but for the second I'll get the last double quote in my extracted string (eg. -------Embt-Boundary--1261032946D275CF").

Solution:

I believe flutter is Dart, which uses JavaScript regex specification, yes? Use capture groups in this way:

boundary=("?)(.*)(\1)

Group 1 will capture a quote if there is one. Group 2 will capture your desired result. Then, \1 will match the quote, if there was one, or nothing, if the quote was not present.

regex101

Answer

Login


Forgot Your Password?

Create Account


Lost your password? Please enter your email address. You will receive a link to create a new password.

Reset Password

Back to login