Click here to Skip to main content
15,748,748 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi there i'm using regular expressions to replace "/" eg in string 1/2 and change into a fraction into xml example frac12 but i want all the fractions this is what i have so far...

public static string ChecknReplace(string ingredientsText)
            string pattern = @"(\d{1}\\\d{1})";
            string[] fractions = {"/"};
            var matches = Regex.Matches(pattern, ingredientsText);
            foreach(Match match in matches)
[no name] 30-Mar-14 13:36pm    
Okay and did you have sort of a question or problem regarding this code?
Nico_Travassos 30-Mar-14 13:38pm    
im not sure how to finish off the foreach statement because everything i try does not work
Kornfeld Eliyahu Peter 30-Mar-14 13:43pm    
Do you mean to replace n/m to fracnm everywhere? (n and m are numbers)
Nico_Travassos 30-Mar-14 13:47pm    

1 solution

Regex regex = new Regex(@"(\d+)/(\d+)");
string result = regex.Replace(InputText,"frac $1 $2 ");
Share this answer
Nico_Travassos 30-Mar-14 14:17pm    
so i must not use the foreach statement
OriginalGriff 30-Mar-14 14:24pm    
It's not "must not" - more "don't have to" :laugh:

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