1

I need to get the string between by_ and _on.
So far I have this, but don't understand how to truncate the actual "string delimiters":
by_(.*)_on

Sample input:
Files_by_wesasegeaazedude_on_January_26.jpg
Current Match:
by_wesasegeaazedude_on
Needed Match:
wesasegeaazedude

Roko C. Buljan
  • 196,159
  • 39
  • 305
  • 313
JESTech
  • 145
  • 2
  • 15

3 Answers3

1

You can simply use positive lookarounds:

String regex = "(?<=by_).*(?=_on)";  

What this regex does is:

  1. match anything: .*
  2. that is preceded by by_: (?<=by_)
  3. and followed by _on: (?=_on)
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
Maljam
  • 6,244
  • 3
  • 17
  • 30
  • 1
    Why do you suggest an inefficient pattern? OP has got the same pattern that is way more efficient. – Wiktor Stribiżew Apr 15 '16 at 16:48
  • @WiktorStribiżew because he is saying that he doesn't want to match the `by_` and `_on` – Maljam Apr 15 '16 at 16:50
  • @Maljam thanks! it worked – JESTech Apr 15 '16 at 17:45
  • Worked or not, _Anytime you lead a regular expression by an assertion, you instantly add %200 more overhead._ This can increase by as much as %10-20,000 depending on what follows it. –  Apr 15 '16 at 18:02
  • @sln so, which expression do you propose? It's good you know all that technical stuff but a quick helping hand is better appreciated most of the time. – JESTech Apr 15 '16 at 18:32
  • @DevJE - You choose ([Benchmark app](http://www.regexformat.com/scrn7/s7_b4.jpg)). _Regex1: (?<=by_).*(?=_on) Elapsed Time: 2.46 s, 2460.14 ms, 2460140 µs Regex2: by_(.*)_on Elapsed Time: 0.39 s, 391.92 ms, 391916 µs_ –  Apr 15 '16 at 18:50
  • @sln you are right, it is more efficient technically, but you are using an example that intentionally chooses a really bad scenario, but if you are using it for file paths, or other small strings, the difference if negligible.. – Maljam Apr 15 '16 at 21:27
1

Your expression is good*. All you need to do is extracting the content of the first capturing group:

Pattern regex = Pattern.compile("by_(.*)_on");
String str = "Files_by_wesasegeaazedude_on_January_26.jpg";
Matcher m = regex.matcher(str);
if (m.find()) {
    String res = m.group(1);
}

Demo.

* Well, almost good. If you expect inputs with multiple file names on the same line, you may want to consider using reluctant qualifier, i.e. by_(.*?)_on

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
1

I would do this without regular expressions.

int start = str.indexOf("by_");
int end = str.indexOf("_on", start + 1); // or lastIndexOf("_on"), for greedy match.
assert start > 0 && end > start;
String part = str.substring(start + 3, end);
Andy Turner
  • 137,514
  • 11
  • 162
  • 243