Tarun Maganti
Tarun Maganti

Reputation: 3076

How to match such kind of strings using and in regex?

How to make an or and and together in Regex.

We can do this in regex (Boo)|(l30o) and list all permutations which basically beats the purpose of using regex. Here or is being used.

I want to match B in any form, O in any form twice. Something like, [(B)|(l3)][0 o O]{2}. But, in this form, it matches (0O too.

O twice matching isn't a problem.

B when trying to match with multiple character match is a problem along with single character match.

Should match: Boo b0o l300 I3oO B00

etc.

All words which look like Boo, i.e., b - {B,b,l3,I3,i3} and o - {O, o, 0};

Upvotes: 3

Views: 140

Answers (3)

Thomas
Thomas

Reputation: 88707

You could try (?:[bB]|[lIi]3)[0Oo]{2}:

  • (?:...) is a non-capturing group
  • [...] is a character class, i.e. any character inside it (except - depending on the position) will be assumed to be meant literally (i.e. [iIl] matches i, L or l, while [(B)|(l3)] wouldn't do what you think it does: it matches any of (, B, ), |, l or 3).
  • | means "or" and matches entire sequences
  • {...} is a numeric quantifier (i.e. {2} means exactly twice)

You could also use (?i) at the start of your expression to make it case-insensitive, i.e. the expression would then be (?i)(?:b|[li]3)[0o]{2}.

Upvotes: 3

Harald Gliebe
Harald Gliebe

Reputation: 7544

Can you try the following

(B|b|l3|I3|i3)[0oO]{2}

You can try it online at https://regex101.com/r/gLA6N2/3

Upvotes: 2

HopefullyHelpful
HopefullyHelpful

Reputation: 1799

(B|b|l3|I3|i3)(O|o|0)+

() is a group

| is an or

+ is a quantifier for {1,} which means 1 or more

Upvotes: 1

Related Questions