Click here to Skip to main content
15,883,531 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi All,

I have string ,
str=Fourty Four Thousand Hundred Sixty Five Dirhams and Twenty Two Fils

I need to remove 'Hundred' from string if the word 'Hundred' is followed by Thousand or Hundred or Lakh.
How to do this.Any help will be really appreciated.

What I have tried:

if (Regex.IsMatch(str, "Hundred"))
               {
                   // check if it is followed by thousand or Hundred or Lakh
                   //remove it if it is followed by thousand or Hundred or Lakh
               }
Posted
Updated 22-Nov-17 7:37am

If you mean "if the word 'Hundred" follows Thousand or ..." as in your example, then try this:
(?<=(Thousand|Hundred|Lakh)\s+)Hundred
To remove the word:
C#
public static Regex remove = new Regex("(?<=(Thousand|Hundred|Lakh)\\s+)Hundred", RegexOptions.Compiled);
...
string result = remove.Replace(InputText,"");

If you want to play with regexes, get a copy of Expresso[^] - it's free, and it examines and generates Regular expressions.
 
Share this answer
 
v2
Comments
Member 12926744 22-Nov-17 4:50am    
However I am getting an error 'too many arguments' while giving this code.
OriginalGriff 22-Nov-17 5:02am    
It was obvious: a superfluous close bracket.
Member 12926744 22-Nov-17 6:34am    
Thank you.It worked.One more thing how to eliminate the extra white spaces before the word hundred because after removing the word there are three white spaces after Thousand.
OriginalGriff 22-Nov-17 6:59am    
Oh come on! That's not exactly complex, if you are using regexes!
Just move the whitespace outside the excluded prefix group...

(?<=(Thousand|Hundred|Lakh))\s+Hundred
Pete O'Hanlon 22-Nov-17 5:32am    
A minor correction, use string.Empty, not "".
If You are too lazy to learn regex , here is another not so elegant solution :

string Check(string str)
		{
			string s = str.Replace(" ","").Replace("Hundred","*");
			
			string [] s1 = s.Split('*');
			
			if(s1[1].Contains("Thousand") || 
			   s1[1].Contains("Hundred")  || 
			   s1[1].Contains("Lakh"))
			{
				str = str.Replace("Hundred","");
			}
			
			return str;
		}


After string check,
word Hundred will be removed if it is followed by words,
Thousand or Hundred or Lakh.

All the best,
Željko Perić
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900