1

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.

Asaph
  • 159,146
  • 25
  • 197
  • 199
Radek
  • 13,813
  • 52
  • 161
  • 255
  • huh? This doesn't look like java code or regex code. – Asaph Feb 06 '13 at 04:50
  • What do you want the "AND" operation to do? Find lines in the document that meet both expressions? Find a piece of text that matches both? It's a little bit ambiguous... would you mind clarifying? Can you give an example? – Floris Feb 06 '13 at 04:51
  • @Asaph,@Naveed see the links at the top of my question for more info on `RegularExpression` – Radek Feb 06 '13 at 04:59
  • I don't thing this is possible in general case, but I can't give a concrete example of two regexps which can't be combined, so I could be wrong. Can you set extra constraints on the regexps? Like, no overlapping matches, then it's easy. – hyde Feb 06 '13 at 05:01
  • @Floris: I need the result of RegExp and RegExp2 to be both true. That means that the documentName will contain both text `eduId=123` and text`some_text` – Radek Feb 06 '13 at 05:02

4 Answers4

3

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!

Patashu
  • 21,443
  • 3
  • 45
  • 53
2

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.

hyde
  • 60,639
  • 21
  • 115
  • 176
  • I like your edit - I am going to delete my own answer now since you addressed everything. I think if you change your `regexpAnd` expression to have a third parameter (possible value "AND" or "OR"), and modify the code inside accordingly, you will have a really awewome function. – Floris Feb 06 '13 at 13:32
1

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

Anirudha
  • 32,393
  • 7
  • 68
  • 89
  • It looks like I wasn't clear. Sometimes I need to use `RegEpx` only, sometimes I need to use `RegExp2` only and sometimes I need to use `RegExp AND RegExp2` in `find` command. It looks like `"eduId="+edu_id+"some_text"` is the way to go. I'm going to try... – Radek Feb 06 '13 at 05:09
  • @Radek you can use if condition like `if(RegExp.find() || RegExp2.find())` or `if(RegExp.find() && RegExp2.find())` – Anirudha Feb 06 '13 at 05:15
  • I need to use RegExp in `find` command - `find(atDescendant(".documentName",RegExp)` I need one single regExp for `regExp and regExp2`. And I don't think that `"eduId="+edu_id+"some_text"` is the correct way how to approach this. – Radek Feb 06 '13 at 05:20
1

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);
}
Yamikuronue
  • 746
  • 8
  • 37