1

target sentence:

$(SolDir)..\..\ABC\ccc\1234\ccc_am_system;$(SolDir)..\..\ABC\ccc\1234\ccc_am_system\host;$(SolDir)..\..\ABC\ccc\1234\components\fds\ab_cdef_1.0\host;    $(SolDir)..\..\ABC\ccc\1234\somethingelse;

how should I construct my regex to extract item contains "..\..\ABC\ccc\1234\ccc_am_system"

basically, I want to extract all those folders and may be more, they are all under \ABC\ccc\1234\ccc_am_system:

$(SolDir)..\..\ABC\ccc\1234\ccc_am_system\host\abc;
$(SolDir)..\..\ABC\ccc\1234\ccc_am_system\host\123\123\123\123; 
$(SolDir)..\..\ABC\ccc\1234\ccc_am_system\host;

my current regex doesn't work and I can't figure out why

\$.*ccc\\1234\.*;
user97662
  • 942
  • 1
  • 10
  • 29

3 Answers3

1

Your problem is most likely that * is a greedy operator. It's greedily matching more than you intend it to. In many regex dialects, *? is the reluctant operator. I would first try using it like this:

\$.*?ccc\\1234.*?;

You can read up a bit more on greedy vs reluctant operators in this question.

If that doesn't work, you can try to be more specific with the characters you match than .. For example, you can match every non-semicolon character with an expression like this: [^;]*. You could use that idea this way:

\$[^;]*ccc\\1234[^;]*;
Community
  • 1
  • 1
Welbog
  • 59,154
  • 9
  • 110
  • 123
0

The below regex would store the captured strings inside group 1.

(\$.*?ccc\\1234\\.*?;)

You need to make the * quantifier to does a shortest match by adding ? next to * . And also this \.* matches a literal dot zero or more times. It's wrong.

DEMO

Avinash Raj
  • 172,303
  • 28
  • 230
  • 274
0

I found this to be the best:

\$(.[^\$;])*ccc\\1234(.[^\$;])*;

it doesn't allow any over match whatsoever, if I use ?, it still matches more $ or ; more than once for some reason, but with above expression, that will never be case. Still thanks to all those who took the time to answer my question,.

user97662
  • 942
  • 1
  • 10
  • 29