dwwilson66
dwwilson66

Reputation: 7066

Will this regex to find bracketed/parenthesized terms in a string?

Consider: "\[|\(phone\)\]"

We are constructing a metadata field, and need to flag filenames that contain the term "phone". Case standards don't exist, so I could see any variation of mixed or single case (phone Phone PHONE etc.) There may or may not be spaces to delineate the words; sometimes there are underscores--so I can't use word boundaries. In ALL cases, the word phone is included in either brackets [] or parentheses ().

The regex I'm trying to construct for a Powershell script will look for a pattern of [ or ( followed by case-insensitive photo and ending with ] or ).

"\[|\(phone\)\]" <-- Is that what I've got here?

Upvotes: 0

Views: 150

Answers (2)

MildlySerious
MildlySerious

Reputation: 9170

I'd do it by accepting one of two possible solutions,

\[phone\]|\(phone\)

This way you match either (phone) or [phone], but not [phone) and (photo].

You would have to set a flag to match case insensitive, though. in Javascript for example, you would add an i behind the delimiters, like /[phone]|(phone)/i

Upvotes: 1

nhahtdh
nhahtdh

Reputation: 56809

Powershell uses .NET Regular Expression, so it has support for (?i). But it turns out the Powershell -match and -replace, and so are -imatch and -ireplace are case-insensitive by default. So the regex doesn't have to be too complex:

\[phone\]|\(phone\)

It will match the text such as: [phone] or (PhONe)

Note that regex is very specific. In this case, no space is allowed in between the () or [] and phone.

Upvotes: 1

Related Questions