Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex AND operator

Based on this answer

Regular Expressions: Is there an AND operator?

I tried the following on http://regexpal.com/ but was unable to get it to work. What am missing? Does javascript not support it?

Regex: (?=foo)(?=baz)

String: foo,bar,baz

like image 824
user366735 Avatar asked Jun 14 '10 22:06

user366735


People also ask

Is there a regex and operator?

In Chain Builder, you can use regular expression (regex) operators to match characters in text strings, such as to define patterns for: Mapping transformation rules for a Data Prep connector pipeline. The File Utilities connector's Find, Find and replace, and Split file commands.

What does operator mean in regex?

The Range Operator ( - ) Regex recognizes range expressions inside a list. They represent those characters that fall between two elements in the current collating sequence. You form a range expression by putting a range operator between two characters.

What does _ do in regex?

The _ (underscore) character in the regular expression means that the zone name must have an underscore immediately following the alphanumeric string matched by the preceding brackets. The . (period) matches any character (a wildcard).

What are the operators used in regular expression?

Operators used in regular expressions include: Union: If R1 and R2 are regular expressions, then R1 | R2 (also written as R1 U R2 or R1 + R2) is also a regular expression. L(R1|R2) = L(R1) U L(R2). Concatenation: If R1 and R2 are regular expressions, then R1R2 (also written as R1.


1 Answers

It is impossible for both (?=foo) and (?=baz) to match at the same time. It would require the next character to be both f and b simultaneously which is impossible.

Perhaps you want this instead:

(?=.*foo)(?=.*baz) 

This says that foo must appear anywhere and baz must appear anywhere, not necessarily in that order and possibly overlapping (although overlapping is not possible in this specific case because the letters themselves don't overlap).

like image 116
Mark Byers Avatar answered Sep 21 '22 10:09

Mark Byers