1

Possible Duplicate:
Find a pattern to match ‘a’, ignoring that ‘a’ which lies within ‘b’ and ‘c’

If I have the following string (for instance)

string q = "select field1 from t1 where field1 in (select field1 from t2)";

In this case I have

a = " from"; b = @"\(.* from.*\)"; => " from" within parenthesis

You can see 'a' is a sub-string of 'b' here

The closest pattern I have is

string pat = @"^((?!\(.* from.*\)).)* from((?!\(.* from.*\)).)*$";

But I need 'pat' be modified a 'a' such that 'a' is not a sub-string of 'b'... ('a' and 'b' are variable patterns). I am stuck at translating this in regular expression. I want to use

string answer=Regex.Relace(q , pat , ",field2 from");

Desired output: (value in answer should be)

"select field1,field2 from t1 where field1 in (select field1 from t2)"
Community
  • 1
  • 1
Sami
  • 8,168
  • 9
  • 66
  • 99
  • Please _format_ your code _as_ code (indent 4 spaces, or use the code button on the editor). – Oded Aug 10 '12 at 19:44
  • so you only want to replace the first ocurance? – Sam I am says Reinstate Monica Aug 10 '12 at 19:48
  • I am using it for a generic purpose infact i need the pattern such that replace any " from" which is not in the parenthesis... You know its only one in an sql query It may not necessarily be first one.. But if it is not in the parenthesis then it is the only one possible where far i know – Sami Aug 10 '12 at 20:01
  • @SamiAkram your ? is a coloumn name..right? – Anirudha Aug 11 '12 at 03:43
  • Thanks @Oded for making my post presentable. And Thanks for the up vote..(I think it was Anirudha) Please help me out to find and relace this particular " from" with regular expression The only condition is that it should not be like (*from) And please help me to post questions. It was first one and i got banned due to my confused presentation. I begged people to guide me rather than down-voting but have to delete it twice due to down-voting and got banned – Sami Aug 12 '12 at 21:23

3 Answers3

2

Why not include t1 in your matching pattern?

string pattern = " from t1";
string answer=Regex.Relace(q , pattern , ",field3 from t1");
Firoz Ansari
  • 2,505
  • 1
  • 23
  • 36
1

You're trying to limit the number of replacements, it sounds, so it would seem like this question has already been answered here:

How to Regex search/replace only first occurrance in a string in .NET?

[EDIT:] Reply to first comment:

Sorry, didn't know if the FROM was an example or what you were actually trying to do. Trying to replace somethign that's not based on a controllable string is problematic. If you don't control all the strings going in, you may also have to handle the case of something like this:

SELECT [From], [To], [Email], [Subject] FROM EmailsSent

End result is that your regex is going to have to look ahead and behind to check for brackets and parenthesis, since you don't 100% control the string it's given. I would put a note of caution that what you are doing sounds like a bad practice and should only be done if there are no alternatives. Take a look at look-ahead/behind information, you should be able to google more on it:

http://openmymind.net/2011/2/16/Regex-Positive-Negative-Look-Ahead-Behind/

Community
  • 1
  • 1
Kevin Nelson
  • 7,613
  • 4
  • 31
  • 42
  • I am using it for a generic purpose infact i need the pattern such that replace any " from" which is not in the parenthesis... You know its only one in an sql query It may not necessarily be first one.. But if it is not in the parenthesis then it is the only one possible where far i know – Sami Aug 10 '12 at 19:58
  • @kevin Nelson **no need** of `look ahead` and `look behind`.He just wants to replace the first `from` clause,so using `$`(end of a string) would be sufficient for the purpose! – Anirudha Aug 11 '12 at 03:41
  • @Anirudha - He specifically said that it "may not necessarily be first one" in the comment just above yours...e.g. there may be a sub-query before the FROM table in the SQL, e.g. SELECT (SELECT col FROM tab2) AS Num FROM tab1). So, you have to check to make sure there are no opening parenthesis before that are not already closed. This is a much more complex regex than just finding the first FROM, which is what I had already suggested before my edit. – Kevin Nelson Aug 13 '12 at 18:26
0
string q = "select field1,field2 from t1 where field1 in (select field1 from t2 where t2.field2>5)";

Regex r=new Regex(@"from.*$");
string s=r.Replace(q,",field3 $0");//output
Anirudha
  • 32,393
  • 7
  • 68
  • 89