score:1
Accepted answer
you can't do that only with regex and a simple search & replace because you'll have to do maths.
here's one of the algorithm that does what you want to do:
int position = 70;
list<string> output = new list<string>();
foreach (string line in file.readalllines(@"c:\users\me\desktop\test.txt"))
{
// the line contains comments and code
if (line.contains(@"//") && !regex.ismatch(line, @"^\s*//"))
{
var matchcollection = regex.matches(line, @"(?<code>.*;)\s*(?<comment>\/\/.*)");
string code = matchcollection[0].groups["code"].value;
string comment = matchcollection[0].groups["comment"].value;
output.add(code.padright(position) + comment);
}
else
{
output.add(line);
}
}
file.writealllines(@"c:\users\me\desktop\out.txt", output);
it's written in c#, since it's pretty close to pseudo code you'll be able to port it easily (at least i hope). here are the main nontrivial parts (do not hesitate if you need more explanations):
regex.ismatch(line, @"^\s*//")
: find the lines that are just comments(?<code>.*;)
: a named capture groupcode.padright(position)
: padrigth add white space to the right of the string up toposition
given this input:
//--------------------------------------------------------------------------!
// unveil left to right !
//--------------------------------------------------------------------------!
static void punveill(ulong frame, ulong field)
{
frame* thisframe = &frames[frame] ; // make code more readable
field* thisfield = &fields[frames[frame].fields[field]] ; // make code more readable
the program outputs:
//--------------------------------------------------------------------------!
// unveil left to right !
//--------------------------------------------------------------------------!
static void punveill(ulong frame, ulong field)
{
frame* thisframe = &frames[frame] ; // make code more readable
field* thisfield = &fields[frames[frame].fields[field]] ; // make code more readable
Source: stackoverflow.com
Related Query
- find and replace in eclipse "go to nth character position"
- eclipse - find and replace underscore character in variables names
- Using eclipse find and replace all to swap arguments
- Find and Replace text in Eclipse
- Eclipse regex find and replace
- Eclipse change case in regex find and replace
- Keyboard shortcut for "Replace/Find" while doing a find and replace in Eclipse
- Eclipse find and replace
- How to find and replace System.out.println with log.info using reg expression in eclipse
- Find and replace any possible instances of "StringVar1 == StringVar2" with "String1.equals(String2)" in a large Java codebase with Eclipse
- Eclipse : Find a line of text and replace it with two lines of text
- RegEx for Find and replace all ArrayList<something> with ArrayList<> in eclipse
- Eclipse - Find and Replace Dialog Always Opening Maximized
- Eclipse find and replace with regex is not working with "(?=regex)"?
- Replacing just the match of regular expression in Eclipse find and replace
- Regex: find occurrences and replace in Eclipse
- Eclipse regex find and replace
- Find and replace line matching a pattern in eclipse
- What are the Find and Replace Symbols/Logic in Eclipse IDE for Java Developers 2020-09?
- regex find and replace a specific attribute from a specific tag eclipse
- Use regex find and replace with macro or shortcut in eclipse
- How to create a project from existing source in Eclipse and then find it?
- How to navigate to the last cursor position in Eclipse if it is in the same file and was not edited?
- Scala Error: Could not find or load main class in both Scala IDE and Eclipse
- Debugging with Eclipse CDT and GDB: can't find source file
- Is it possible in Eclipse to highlight a statement, and then press quotes/etc.. to "wrap" the highlighted text in the character (like in SublimeText)?
- eclipse search and replace with wildcard
- Is there an Eclipse plugin that can replace the use of external tools such as Putty and WinScp
- How do I get Eclipse to find and use a gcc based toolchain in a non-standard location
- Eclipse / Aptana regex search and replace
More Query from same tag
- Java XMLInputFactory Error
- Why does the Eclipse Android IDE get so slow and laggy after only a small amount of use?
- Button invisble but enabled
- Where do the "Warnings" in Eclipse come from?
- Eclipse RCP Target can't Add Feature because jar file not found in bundle pool
- "Close unrelated projects" function gone in Eclipse
- Eclipse and jre link. Java prog runs without jre link in eclipse
- NSIS Eclipse plugin not working in Helios and Linux platform
- EJBConfigurationException: Bean class for EJB not found
- Problem installing Maven plugin (m2eclipse) in Eclipse (Galileo)
- build asmack on windows
- Eclipse For Android - Troubleshooting Android Virtual Device
- How to merge the two cells(columns) of cross-tab in BIRT eclipse design?
- Eclipse color does not change
- Issues with Eclipse while working on and Android project
- Web Service client - Service definition selected is invalid
- Java True Global Variables (Eclipse for Android)
- OSGI bundle: NoClassDefFoundError
- EditText -Cannot invoke getText() on the primitive type int
- "read-only file system" error when writing to SD card
- Eclipse code formatting: is there a way to add "section lines"
- Cannot emulate Android device in Eclipse
- Where is the definition for a new Python interpreter in Eclipse Pydev stored?
- Failed to read schema document 'http://www.springframework.org/schema/beans/spring- beans-4.0.xsd'?
- How to add RXTX serial API's javadoc to eclipse or netbeans?
- How do I run DataNucleus from within an Eclipse executable JAR?
- Eclipse: detach package explorer, outline panels?
- Eclipse: The declared package does not match the expected package
- JUnit test won't run when I Run-As from the test.java file
- Eclipse isn't using allotted heap; I get an out of memory error?