Radek
Radek

Reputation: 11121

How to code "regexp1 and regexp2" as regular expression?

Currently my java code in RFT(Rational Functional Tester)


see Class RegularExpression and search for text "RegularExpression" this page on to use RegularExpression.


looks like

RegularExpression RegExp = new RegularExpression("eduId="+edu_id, false);
to = find(atDescendant(".documentName",RegExp));

Now I need to add another regular expression

RegularExpression RegExp2 = new RegularExpression("some_text", false);

and then I need to combine these two regular expressions using AND

something like RegularExpression RegExp_final = RegExp && RegExp2;

so I can use the final one in find command. to = find(atDescendant(".documentName",RegExp_final));

Could somebody help me with the syntax, how to write this down?

Sometimes I need to use one or the other regular expression and sometimes both of them together.

Upvotes: 1

Views: 1031

Answers (4)

Yamikuronue
Yamikuronue

Reputation: 758

I need to use RegExp in find command - find(atDescendant(".documentName",RegExp)

We solve this issue using code along the following lines:

TestObject[] results = find(atDescendant(".documentName",RegExp)
if (results.length == 0)
    results = find(atDescendant(".documentName",RegExp2)

Be sure to put the most common case first, so you can properly shortcut.


I've just noticed you also said:

I need the result of RegExp and RegExp2 to both be true

in that case you want to iterate over the result array above, comparing each match to the second regex:

TestObject[] results = find(atDescendant(".documentName",RegExp)
ArrayList<TestObject> potentials = new ArrayList<TestObject>;
for(TestObject candidate : results) {
    if (Regexp2.matches(candidate.getProperty(".documentName")))
       potentials.add(candidate);
}

Upvotes: 1

hyde
hyde

Reputation: 62817

If you have two valid regexp strings "A" and "B", I think the closest you can easily get is this regexp string (quotes not part of the regexp):

"A.*B|B.*A"

If A or B may contain |, then you probably have to use non-capturing groups like this (untested):

"(?:A).*(?:B)|(?:B).*(?:A)"

Some Java code:

Pattern regexpAnd(Pattern a, Pattern b) {
    return Pattern.compile(
         "(?:" + a.pattern() + ").*(?:" + b.pattern() + ")|"
       + "(?:" + b.pattern() + ").*(?:" + a.pattern() + ")" );
}

At least with any high level language standard regexp library, that's it. If you want more than that, dig out the regexp library sources (or write your own...) and add a way to combine the parse/match trees of compiled regexps... Then produce new regexp string from the resulting tree. But in short: not worth it.

Upvotes: 2

Patashu
Patashu

Reputation: 21773

Just do a search for both regexes. There's not a good way in general to specify a regex that is the AND of two regexes, because... you could just run both! (You could try some stuff with lookbehinds, lookaheads and concatenation, but it would get messy)

If the method only allows you to pass one regex, write one that lets you pass two. Or a collection! Or a callback that can decide however it likes if this is a match or not - then you might not even need to use regexes inside of it!

Upvotes: 3

Anirudha
Anirudha

Reputation: 32807

Use | to join multiple regex..

so it would be like

"eduId="+edu_id+"some_text"+"|"+"eduId="+edu_id+"|"+"some_text"
--------------------------  ---                 ---
         |->AND              |->OR               |->OR

For example in the below regex

a|b|c|abc

either a or b or c or abc(AND) would be matched

Upvotes: 1

Related Questions