-1

The expression I have is

\((from \/projects(\/(\w|.)+)+):$(\d+\))

The strings I'm trying to match are:

(from /projects/idpro/branches/release_2018/test/test/test.1:13118)
(from /projects/idpro/trunk:13074)

the online regex testing utility I'm using blows up stating catastrophic backtracking. So here I am trying to learn what backtracking is, so I can avoid it and fix my code.

Of the 2 string examples I have provided, I would like to match both. In the first case, I'm trying to match (from /projects. After the word projects, I want to ignore a path of any dept, so long as the string ends with a :\d+\)

Similarly, for the second example, I want a match if (from /projects is followed by at least one folder, and ends with a :\d+\).

AdThanksVance

lyxαl
  • 1,108
  • 1
  • 16
  • 26
M. Khan
  • 77
  • 1
  • 9

1 Answers1

0

I'm not sure exactly what is causing the catastrophic backtracking. But in any case, I would phrase your regex as this:

^\(from \/projects(?:\/[^\/]+)*:\d+$

As you can see in the demo below, there is no catastrophic backtracking happening.

Demo

Catastrophic backtracking is usually associated with poorly written regex patterns. So, just don't do that, and it probably won't happen.

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360