ozczecho
ozczecho

Reputation: 8849

RegEx to ignore square brackets

My Regex is (?:[^\S\n]|[^[:cntrl:]])*

If I try and match Benjamin Edward Ben it matches the whole string, ie "Benjamin Edward Ben".

If I try and match text with square brackets, ie: "Benjamin Edw[ard] Ben" it matches up to the first square bracket, ie "Benjamin Edw".

If I try and match text with any other text, ie "Benjamin Edw*ard^ Ben" it matches the whole string, ie "Benjamin Edw*ard^ Ben".

How do I change my regex so it matches the whole string even if it has square brackets?

Thanks in advance.

Upvotes: 2

Views: 2723

Answers (3)

Anirudha
Anirudha

Reputation: 32787

The problem is with [:cntrl:]..Change it to \p{Cc}

[:cntrl:] class format is not supported in .net

\p{Cc} would match control characters similar to [:cntrl:]

Your regex would be

 (?:[^\S\n]|[^\p{Cc}])*

which is similar to

[^\S\n\p{Cc}]*

NOTE

[^\S\n] means matching space characters except newlines.So with above regex you would also match spaces

Upvotes: 2

tr33hous
tr33hous

Reputation: 1642

To match string only, I would advise simplifying to ([\w\s\[\]]+). Further clarification would be appreciated

Upvotes: 1

Praveen
Praveen

Reputation: 56501

Try using \\ before the square bracket in your regex.

Upvotes: 0

Related Questions