Matthew Layton
Matthew Layton

Reputation: 42300

Regex find at least one of two characters

Bear with me, I am new to regular expressions, so my syntax may be slightly out.

Here is my expression:

"(?:\\s*[\"]?[']?\\s*)"

Which equates to: Any amount of white space, then the possibility of a double quote, then the possibility of a single quote, then any amount of white space.

The problem I have is that this still matches even if there is no double quote or single quote.

How do I make my expression so that there must be at least 1 double quote OR at least 1 single quote?

Upvotes: 3

Views: 1382

Answers (3)

juharr
juharr

Reputation: 32296

If you mean you want to find one single or one double quote then just put both inside a character group and don't put a question mark after it.

(?:\s*[\"']\s*)

If you mean you want 1 or more single quotes or 1 or more double quotes

(?:\s*([\"]+)|([']+)\s*)

If you mean you want 1 or more single or double quotes

(?:\s*[\"']+\s*)

Upvotes: 1

Stefano Altieri
Stefano Altieri

Reputation: 4628

This should do the work:

@"(?:\s*('|\")+\s*)"

Upvotes: 4

Dave Bish
Dave Bish

Reputation: 19646

Try this expression:

(?:\\s*[\\"\\']\\s*)

:D

Upvotes: 1

Related Questions