4

So I'm not sure if I understand how this works and would like a simple explanation to how they work is all. I probably have it way off. A pure regex solution is required, and I don't know if this is possible. If it is, a solution would be awesome too, but a shove in the right direction would be good for my learning process ^_^

This is how I thought the if/then/else option built into my regex engines was formatted:

?(condition)if regex|else regex

I want it to capture a string from a very specific location only when this string exists within a certain section of javascript. Because this is how I thought it worked after a decent amount of research I tried out a few variations of this code but they all ended up something like this.

((?^view_large$)Tables-137(.*?)search.htm)

Also of relevance: I'm using an java based app that has regex searches which pull the data I need so I cannot write an if statement in java which would be my preferred method. It's a pain to have to do it this way, but at the moment I have no other choice. I'm trying really hard for them to allow java code functionality instead of pure regex for more versatile options.

So to summarize, is there even a if/then option in regex and if so how is it formatted for what I'm trying to accomplish?

EDIT: The string that I want to be the "if condition" is like this: if view_large string exists and is not null then capture the exact string 500/ which is captured within the catch all group I used: (.*?)

Travis Crum
  • 399
  • 2
  • 5
  • 21

3 Answers3

10

There is no conditionals in Java regexp, but you can simulate them by writing two expressions that include mutually exclusive look-behind constructs, like this:

((?<=if )then)|((?<!if )end)

This expression will match "then" when it is preceded by an "if "; it will match "end" when it is not preceded by an "if "

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • awesome this is exactly what I want it to do. What would I do if the `"if"` has to be an exact string? – Travis Crum Oct 02 '12 at 13:55
  • @TravisDtfsuCrum In my example `"if "` is the exact string. For example, this expression `((?<=a)b)|((?<!a)c)` will match `b` if there's `a` in front of it, or `c` if there's anything except `a` in front of it. – Sergey Kalinichenko Oct 02 '12 at 13:58
  • awesome, perfect perfect perfect. Wasn't sure if it took the input in a look-behind construct as literal or like any other regex – Travis Crum Oct 02 '12 at 14:02
  • would a lookahead be formatted like this then? `((?=if )then)|((?!if )end)` – Travis Crum Oct 02 '12 at 14:05
  • @TravisDtfsuCrum Look-behind expressions can take literals, but are not limited to them: for example, `(?<=[a-f])x` would be a valid regexp, matching `x` when it is preceded by `a`, `b`, `c`, `d`, or `f`. – Sergey Kalinichenko Oct 02 '12 at 14:05
  • 1
    @TravisDtfsuCrum Lookaheads are placed after the expression, not before it, so your expression would be `(then(?=if ))|(end(?!if ))` – Sergey Kalinichenko Oct 02 '12 at 14:07
  • Ok thanks man for all the help. This actually saved me a ton of time – Travis Crum Oct 02 '12 at 14:10
  • See http://www.regular-expressions.info/lookaround.html for more information about lookahead and lookbehind. Note in particular that lookbehind usually doesn't allow repetition or optional items. – Barmar Oct 02 '12 at 14:56
2

The Javadoc for java.util.regex.Pattern mentions, in its list of "Perl constructs not supported by this class":

  • The conditional constructs (?(condition)X) and (?(condition)X|Y).

So, no dice. But you should look through the Javadoc to see if you can achieve what you need by using regex features that it does support. (Or, if you post some more detailed examples, we can try to help.)

ruakh
  • 175,680
  • 26
  • 273
  • 307
  • Thanks for the answer, the websites with tutorials I read gave me the impression that it existed but what not sure since nothing I was trying worked – Travis Crum Oct 02 '12 at 13:53
1

Try lookaround assertions.

For example, say you want to capture FOOBAR only if there is a 4+ digit number somewhere:

(?=.*\d{4}).*(FOOBAR)
Andrew Cheong
  • 29,362
  • 15
  • 90
  • 145
  • Thanks for the input, I wasn't sure if lookarounds would do what I wanted since I'm pretty new to regex. Especially when it comes to using pure regex solutions – Travis Crum Oct 02 '12 at 14:00