how to write regular expression for this code?
-
hi, I have below code. how do I replace with regular expression or much sipler to this.? many thanks
if(!identier.StartsWith("L#")
identifier.Replace(something,"");
if(!identier.StartsWith("X#")
identifier.Replace(something,"");
if(!identier.StartsWith("G#")
identifier.Replace(something,""); -
hi, I have below code. how do I replace with regular expression or much sipler to this.? many thanks
if(!identier.StartsWith("L#")
identifier.Replace(something,"");
if(!identier.StartsWith("X#")
identifier.Replace(something,"");
if(!identier.StartsWith("G#")
identifier.Replace(something,"");Looking at your example, the "pattern" that "jumps out at me" is that in each case you are using the first two characters of (what I assume is the string) 'identifier.' That suggests, to me, a switch/case statement using the first two characters of the 'identifier' string as the 'match' criterion. But, if your 'heart is set' on a RegEx solution, don't let me deter you :) best, Bill
"Beauty is in the eye of the beholder, and it may be necessary from time to time to give a stupid or misinformed beholder a black eye." Miss Piggy"
-
hi, I have below code. how do I replace with regular expression or much sipler to this.? many thanks
if(!identier.StartsWith("L#")
identifier.Replace(something,"");
if(!identier.StartsWith("X#")
identifier.Replace(something,"");
if(!identier.StartsWith("G#")
identifier.Replace(something,"");You can't - your code is wrong, on a number of levels. Firstly:
if(!identier.StartsWith("L#")
identifier.Replace(something,"");Is incomplete - the if condition is never terminated (you need a ')' in there. (Assuming you fix that) Secondly: If identifier starts with "L#" then it won't start with "X#". So, your replace operation will be called by at least one of the first two if conditions, and then always by the third. (Assuming you fix that) Thirdly: Strings in .NET are immutable - they cannot be changed - so string.Replace does nothing at all to identifier, but instead generates a new string, which you then discard. As a result, your entire code block as shown can be replaced by nothing, as it has no effect whatsoever on subsequent code! You don't need a regex for that! Think again what you are trying to do, and perhaps explain to us what results you are expecting, becauae that is not going to give them to you. :laugh:
Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together. Manfred R. Bihy: "Looks as if OP is learning resistant."
-
You can't - your code is wrong, on a number of levels. Firstly:
if(!identier.StartsWith("L#")
identifier.Replace(something,"");Is incomplete - the if condition is never terminated (you need a ')' in there. (Assuming you fix that) Secondly: If identifier starts with "L#" then it won't start with "X#". So, your replace operation will be called by at least one of the first two if conditions, and then always by the third. (Assuming you fix that) Thirdly: Strings in .NET are immutable - they cannot be changed - so string.Replace does nothing at all to identifier, but instead generates a new string, which you then discard. As a result, your entire code block as shown can be replaced by nothing, as it has no effect whatsoever on subsequent code! You don't need a regex for that! Think again what you are trying to do, and perhaps explain to us what results you are expecting, becauae that is not going to give them to you. :laugh:
Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together. Manfred R. Bihy: "Looks as if OP is learning resistant."
Certainly your first point and conclusion are correct, but the other points assume a
String
, which may not be a safe assumption.identifier
may be aStringBuilder
(for instance*) and theStartsWith
method may be an Extension Method -- we just don't know, not enough information was provided. If in fact your third point is incorrect, andidentifier
is mutable, then your second point is also not valid. Having said that, I wouldn't bet against your being absolutely correct on all points. :sigh: * It could also be some other unknown class. -
Certainly your first point and conclusion are correct, but the other points assume a
String
, which may not be a safe assumption.identifier
may be aStringBuilder
(for instance*) and theStartsWith
method may be an Extension Method -- we just don't know, not enough information was provided. If in fact your third point is incorrect, andidentifier
is mutable, then your second point is also not valid. Having said that, I wouldn't bet against your being absolutely correct on all points. :sigh: * It could also be some other unknown class.I agree with you completely - it's just Occam suggests my answer, given the code quality of the whole fragment :sigh:
Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together. Manfred R. Bihy: "Looks as if OP is learning resistant."
-
hi, I have below code. how do I replace with regular expression or much sipler to this.? many thanks
if(!identier.StartsWith("L#")
identifier.Replace(something,"");
if(!identier.StartsWith("X#")
identifier.Replace(something,"");
if(!identier.StartsWith("G#")
identifier.Replace(something,"");It depends on the declaration of identifier. Assuming it is a string, here is my shortest replacement:
new System.Text.RegularExpressions.Regex(identifier.ToString());
Others already explained why it does the same your code does. And yes, the
ToString()
part is necessary. :)Luc Pattyn [My Articles] Nil Volentibus Arduum
-
It depends on the declaration of identifier. Assuming it is a string, here is my shortest replacement:
new System.Text.RegularExpressions.Regex(identifier.ToString());
Others already explained why it does the same your code does. And yes, the
ToString()
part is necessary. :)Luc Pattyn [My Articles] Nil Volentibus Arduum
Luc Pattyn wrote:
And yes, the
ToString()
part is necessary.Ummm... what? :confused:
-
Luc Pattyn wrote:
And yes, the
ToString()
part is necessary.Ummm... what? :confused:
Exactly what I said. ;P
Luc Pattyn [My Articles] Nil Volentibus Arduum
-
Looking at your example, the "pattern" that "jumps out at me" is that in each case you are using the first two characters of (what I assume is the string) 'identifier.' That suggests, to me, a switch/case statement using the first two characters of the 'identifier' string as the 'match' criterion. But, if your 'heart is set' on a RegEx solution, don't let me deter you :) best, Bill
"Beauty is in the eye of the beholder, and it may be necessary from time to time to give a stupid or misinformed beholder a black eye." Miss Piggy"
-
You can't - your code is wrong, on a number of levels. Firstly:
if(!identier.StartsWith("L#")
identifier.Replace(something,"");Is incomplete - the if condition is never terminated (you need a ')' in there. (Assuming you fix that) Secondly: If identifier starts with "L#" then it won't start with "X#". So, your replace operation will be called by at least one of the first two if conditions, and then always by the third. (Assuming you fix that) Thirdly: Strings in .NET are immutable - they cannot be changed - so string.Replace does nothing at all to identifier, but instead generates a new string, which you then discard. As a result, your entire code block as shown can be replaced by nothing, as it has no effect whatsoever on subsequent code! You don't need a regex for that! Think again what you are trying to do, and perhaps explain to us what results you are expecting, becauae that is not going to give them to you. :laugh:
Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together. Manfred R. Bihy: "Looks as if OP is learning resistant."
my requirement is if a string starts with "L#",or "G#" or "X#" i dont want to replace some part of the string. Otherwise I will replace. for example: string identifier="L#1234"; In this case I dont want to replace any thing there. Otherewise I would replace some part of the string there.
-
my requirement is if a string starts with "L#",or "G#" or "X#" i dont want to replace some part of the string. Otherwise I will replace. for example: string identifier="L#1234"; In this case I dont want to replace any thing there. Otherewise I would replace some part of the string there.
Then why worry about using a regular expression? There's no need to do this - sometimes it's just easier to directly manipulate a string - in this case, if the second character isn't hash then you don't need to check the first character.
Forgive your enemies - it messes with their heads
My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility
-
Then why worry about using a regular expression? There's no need to do this - sometimes it's just easier to directly manipulate a string - in this case, if the second character isn't hash then you don't need to check the first character.
Forgive your enemies - it messes with their heads
My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility
-
I agree with you completely - it's just Occam suggests my answer, given the code quality of the whole fragment :sigh:
Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together. Manfred R. Bihy: "Looks as if OP is learning resistant."
+5 for bringing in my favorite "razor," William of Ockham, as well as, of course, a good, solid, technical answer. best, Bill
"Beauty is in the eye of the beholder, and it may be necessary from time to time to give a stupid or misinformed beholder a black eye." Miss Piggy"
-
Certainly your first point and conclusion are correct, but the other points assume a
String
, which may not be a safe assumption.identifier
may be aStringBuilder
(for instance*) and theStartsWith
method may be an Extension Method -- we just don't know, not enough information was provided. If in fact your third point is incorrect, andidentifier
is mutable, then your second point is also not valid. Having said that, I wouldn't bet against your being absolutely correct on all points. :sigh: * It could also be some other unknown class.+5 for acute analysis thoroughly applied ! best, Bill
"Beauty is in the eye of the beholder, and it may be necessary from time to time to give a stupid or misinformed beholder a black eye." Miss Piggy"