How to use AND in Ruby Regex

I have problems with Ruby regex.

How do you make the regex AND (&) in ruby?

Example:

cat and dog
cat
dog

I just want to match "cat and dog"
+5
source share
5 answers

You can do something like AND using a positive look ahead

(?=.*cat)(?=.*dog).*

See here on Rubular. Updated link!

This positive lookhhead (?=.*cat)checks to see if somewhere inside the string "cat", the same as for "dog" is used (?=.*dog). If both of these statements are true, then the full line is matched with .*at the end.

The advantage is that it will also fit

dog and cat

and not only

cat and dog

but he will also fit

,

(?=.*\bcat\b)(?=.*\bdog\b).*

\b - , .

+13

.

, "cat", "dog" (, ), :

/^.*(cat.*dog|dog.*cat).*$/

" ", "".

, "cat" "dog" , :

/^.*(\bcat\b.*\bdog\b|\bdog\b.*\bcat\b).*$/

" ", " " " ".

: http://ruby-doc.org/docs/ProgrammingRuby/html/intro.html#S5

+2

, , ..

cat AND dog, -

cat.*dog

, "cat" -, "". , , ...

0

, .

wild card

cat.+dog

cat.+dog|dog.+cat

cat +and +dog
0

Regexp s and, , . : String 'cat' 'dog'?

0

All Articles