Java - replace regex in file - java

In java, I have a file where I need to replace text that matches a regex with something that depends on the text matched (it's an assembly file, and I have to replace a method name by its address, stored in a tabular).
Is there a better way to do this than load it to a string, replace in it, and write it again in the file ? I couldn't find anything that can work 'directly' in the file.
Thanks for help,

Related

Regex to check if file does not have an extension

I want to process files based on file extension. I need to process 2 files: one is with .nc extension and another file with does not have any extension
File name could be anything, doesn't matter.
for .nc extension I have .*.nc regex but I need combine regex. I googled but unable to find anything. Could anyone help me with regex which matches these 2 conditions?
You can use this pattern (?(?=.*\.)^.*\.nc$|^.*$)
This is conditional with positive lookahead, which checks if string contains dot (with pattern (?=.*\.)). If it does, then match string with .nc extension (with ^.*\.nc$), if not, then match whole string (with ^.*$).
Demo
You can use the regex (\w+.nc\b|\b\w+\b[^.]). It would capture anything like abc.nc and abc but not abc.rc So it would only capture the required extention or with no extension.
I think this would also do just fine for your case.
^([^\s]+.nc|[^\s.]+)$
.^and$ asserts position at the start and end of line respectively and in between it matches any word character without extension or with .nc extension.

Best way to store words for given scenerio

I am working on Java project [Maven].
I am confused in one point. I don't know what is logiclaly corect.
Problem is as follows :-
Sentence is given, and from their I have extract some particular words.
Solution that I found
I make one regex and put in Constants class. Whenever I have to add more words, I simply appended words in regex.
This solves the problem.
I am confused here
I am thinking, if I put numbers of text files in resources folder where each text file denotes one regex expression.
REGEX = (?:A|B|C|D)
A, B, C, D = Word(String)
Is it a good idea ? If not please suggest any other.
Why would you save regex's in a text file? The fact that you're using a regex seems like an implementation detail that you would want to encapsulate (unless you want the significantly greater functionality but also overhead of supporting regexes).
Also, why do you need new files for each word? That seems like you could just have one file with a word per line that is all of the words you're interested in. This would be much more simple for a user to understand than 100 files with one regex per file.
As my understanding, you want to find some key words from the input string. And those key words could be extened according your requirments.
your current solution is to make this regex (?:A|B|C|D) in your Constant class, wheneveer it's required, you'll add more key words in this regex.
If my understanding is not wrong, maybe, one suggestion is to put this regex in your properties file, like this
REGEX = (?:city|Animal|plant|student)
if too long, it's could be like this
REGEX = (?:city|Animal|plant|student|car|computer|clothes|\
furnature|others)
Your second idea, if my understanding is not wrong, is to put the keywords as the file name, and those files are put in one resource folder. therefore, you could obtain those files name to compose the final regexp. If your regex are always fixed as the (?:A|B|C|D) format, then this solution is good & convenient. (Every time, you add one new keyword file, you don't need to modify any source code & property file)

Using regex to match String to name in external file

I am writing a program that is supposed to detect whether a type name is contained within an external file. For example, if a string is equal to "anitasugarland" and the external file contains the name "ANITA" then is there any way to confirm if there is a name match? The problem I'm having is if I just use Java's "startsWith" then it matches on other names like "An" or other names. As you can see this can cause inaccuracies. So is there a way using regex or word boundaries to check if a first name in the string matches the one in the external file? As of now this really has me stumped. If someone could take a look at this or provide a possible solution I would very much appreciate it!
Thank you!

Issues with path for java.io.File

I am getting file path in eclipse plugin using org.eclipse.swt.widgets.FileDialog and saving the path in XML files.
In web.xml , path is stored as below (I can't change backsladh to forwardslash or escape backslash since the value is coming from SWT FileDialog)
<init-param>
<param-name>filePath</param-name>
<param-value>c:\new\demo\next\version.txt</param-value>
</init-param>
In my filter , i have below code in init() method but am not able to get File reference due to special characters
String filePath = filterConfig.getInitParameter("filePath");
// Tried filePath.replace('\\','/') --> Didnot work since \n is a single character
File f = new File(path)
i could not get the actual path since \n is considered as new line.
That suggests that whatever's reading the file is assuming escaping which simply isn't the case.
Unfortunately, you haven't told us what code is used to write the data or the code used to read the data. Is it under your control in either path?
Basically you need to be escaping in the same way as you unescape - so either you can escape c:\new\demo\next\version.txt to c:\\new\\demo\\next\\version.txt when you're writing the data, or you can remove the code which tries to unescape when you're reading the data.
Note that if you literally tried:
string.replace('\\', '/');
then that certainly won't help at all - as you're ignoring the return value. If you tried
string = string.replace('\\', '/');
then that should have performed the relevant replacements, but you didn't say where you were trying to do that, or in what way it didn't work.
I would actually treat the forward vs back-slashes as a red herring here: fundamentally you need to escape in the same way as you unescape. Replacing backslashes with forward slashes may help for filenames, but you'll just get problems elsewhere in cases where you can't just perform that replacement.
I am getting file path in eclipse plugin using
org.eclipse.swt.widgets.FileDialog and saving the path in XML files.
In web.xml , path is stored as below (I can't change backsladh to
forwardslash or escape backslash since the value is coming from SWT
FileDialog)
The SWT File Dialog does not write web.xml files. You are doing that, so you can fix the backslashes. Change them to forward slashes.
I have below code in init() method but am not able to get File
reference due to special characters
that did not work because \n is considered a single character [from one of your comments]
That implies that the data is wrong in the web.xml file. Nothing you can do in code can fix that. This is not a coding question. You don't need to process backslashes in Java. The compiler already does that, and so in this case did the XML parser in the web-app container. You need to get the file format correct up front.

How to remove particular string from .asp file, using Java?

I'm currently writing something which is validating our vbscript files. Right at the start I wish to remove all lines of code which are comments. I was expecting to be able to use the "'" (comment symbol in vbscript) and '\n'. However, when I write the content of the file to screen, the new lines are not formatting. Does this mean there are actually no new lines in the original vbscript file and if not, how could I remove comments?
first read whole file in string example
then use regex or simply substring for removing extra syntax
How are you parsing the file? Are you also taking the '\r' into consideration when removing the comments? Or maybe you are accidentally removing all newline characters.
I would create some state flags to tell the parser when I was in a comment or not.

Categories