3

I was given a task to do which requires a long time to do.

The image say it all :

This is what I have : (x100 times):

enter image description here

And I need to extract this value only

enter image description here

How can I capture the value ?

I have made it with this regex :

DbCommand.*?\("(.*?)"\);

As you can see it does work :

enter image description here

And after the replace function (replace to $1) I do get the pure value :

enter image description here

but the problem is that I need only the pure values and not the rest of the unmatched group :

Question : In other words :

How can I get the purified result like :

Eservices_Claims_Get_Pending_Claims_List
Eservices_Claims_Get_Pending_Claims_Step1 

Here is my code at Online regexer

  • Is there any way of replacing "all besides the matched group" ?

p.s. I know there are other ways of doing it but I prefer a regex solution ( which will also help me to understand regex better)

Royi Namir
  • 144,742
  • 138
  • 468
  • 792

4 Answers4

2

Unfortunately, JavaScript doesn't understand lookbehind. If it did, you could change your regular expression to match .*? preceded (lookbehind) by DbCommand.*?\(" and followed (lookahead) by "\);.

With that solution denied, i believe the cleanest solution is to perform two matches:

// you probably want to build the regexps dynamically
var regexG = /DbCommand.*?\("(.*?)"\);/g;
var regex  = /DbCommand.*?\("(.*?)"\);/;
var matches = str.match(regexG).map(function(item){ return item.match(regex)[1] });

console.log(matches); 
// ["Eservices_Claims_Get_Pending_Claims_List", "Eservices_Claims_Get_Pending_Claims_Step1"]

DEMO: http://jsbin.com/aqaBobOP/2/edit

Tibos
  • 27,507
  • 4
  • 50
  • 64
1

You should be able to do a global replace of:

public static DataTable.*?{.*?DbCommand.*?\("(.*?)"\);.*?}

All I've done is changed it to match the whole block including the function definition using a bunch of .*?s.

Note: Make sure your regex settings are such that the dot (.) matches all characters, including newlines.


In fact if you want to close up all whitespace, you can slap a \s* on the front and replace with $1\n:

\s*public static DataTable.*?{.*?DbCommand.*?\("(.*?)"\);.*?}

Using your test case: http://regexr.com?37ibi

lc.
  • 113,939
  • 20
  • 158
  • 187
1

You can use this (without the ignore case and multiline option, with a global search):

pattern: (?:[^D]+|\BD|D(?!bCommand ))+|DbCommand [^"]+"([^"]+)
replace: $1\n
Casimir et Hippolyte
  • 88,009
  • 5
  • 94
  • 125
0

Try simply replacing the whole document replacing using this expression:

^(?: |\t)*(?:(?!DbCommand).)*$

You will then only be left with the lines that begin with the string DbCommand

You can then remove the spaces in between by replacing: \r?\n\s* with \n globally.

Here is an example of this working: http://regexr.com?37ic4

JonM
  • 1,314
  • 10
  • 14