I have a long template, from which I need to draw some teaspoons based on some samples. When I went through some examples, I came to know that it is good to use Quantifiers in such situations. For example, my template is from which I need to remove while
and doWhile
This is a sample document whereas $ ($ variable) This text can be repeated multiple times until the call is made. $ EndWhile Here is some sample text. $ While ($ variable2) This text can be repeated multiple times until the call is made. $ EndWhile Some sample text
I need to remove the whole text, which starts with $ ($ variable)
as long as $ endWhile
. Then I need to process the value of $ variable. After that I need to insert the text between the original code in the code between $ while
and $ endWhile
. There is a logic to remove the variable but I am not sure how to use quantifiers or pattern matches here. Can anyone provide me a sample code for this? Any help would be greatly appreciated
You can use a simple simple regex-based solution here with a macro here: < / P>
Pattern Pattern = Pattern.compile ("\\ $ while \\ ((. *?) \\) (. *?) \\ $ endWhile", pattern. DOTALL); Matcher matcher = pattern.matcher (yourString); While (matcher.find ()) {string variable = matcher.group (1); // will contain $ string value = matcher.group (2); // Now do something with variables and values}
If you want to change the variable in the original text, then you should use the / solution:
< Code> pattern pattern = Pattern.compile ("\\ $ while \\ ((. *?) \\) (. *?) \\ $ endWhile", pattern. DOTALL); Matcher matcher = pattern.matcher (yourString); Stringbuffer sb = new stringbuffer (); While (matcher.find ()) {string variable = matcher.group (1); // will contain $ string value = matcher.group (2); // Now do something with variable and value matcher.appendReplacement (SB, value); } Matcher.appendTail (SB);
Comments
Post a Comment