-1

Input:

1.\frac{[a+b]}{xjch}

2.\frac{pqz}{xjch}

Wanted output is

1.[a+b]/(xjch)

2.(pqz)/(xjch)

My regex is:

\\frac\{(.{2,})\}\{(.{2,})\}

if i apply this regex, the output will be,

1.([a+b])/(xjch)

2.(pqz)/(xjch)

But i dont want () in [a+b]. ie if any special character inside the {...}, the round bracket should not come. otherwise, (Without special characters) ,the round bracket should come like (pqz),(xjch).

I want two regex for both 1. and 2. then only i will get wanted output.

Could anyone help me?

nu11p01n73R
  • 26,397
  • 3
  • 39
  • 52
Anitha
  • 111
  • 2
  • 12
  • 1
    I think the parentheses are just representations of the groups in whichever tool you're using? In truth you're only getting [a+b] and xjch for the first one, and pqz and xjch for the last one. – Tyress Oct 15 '14 at 09:28
  • How does one add parentheses by "applying" a regex? – Jongware Oct 15 '14 at 09:29
  • To do a replacement with conditions in C#, you need to use a callback function, see this post: http://stackoverflow.com/questions/442245/c-sharp-replace-with-callback-function-like-in-as3 – Casimir et Hippolyte Oct 15 '14 at 09:30

1 Answers1

0

you can write a Regex that contain within the bracket and replace the group 1 and 2 with a condition

if(nextchar == "[") 
    TypeOfYourInstuction = 1; 
else 
    TypeOfYourInstuction = 2;` 

and this regex is

\\frac\{\[?([a-zA-Z1-9\+]{2,})\]?\}\{\[?([a-zA-Z1-9\+]{2,})\]?\}

http://regex101.com/r/dN8sA5/18

but as you mention it, you can write two regex for first type and the second one:

the first regex: \[[^\]]{2,}\] // Demo = http://regex101.com/r/dN8sA5/20
the second regex: \{[^\[^\}]*\}  // Demo = http://regex101.com/r/dN8sA5/19

you have to replace the second type with parenthesis

Mehdi Khademloo
  • 2,754
  • 2
  • 20
  • 40