Using regex to match String to name in external file - java

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!

Related

Java - replace regex in file

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,

How to search for a string and append to each occurance in Java

Currently I am working on a project and I am trying to see which String method would be most appropriate to use or how to approach this. I am trying to prepend a string to each occurrence of this specific string. For example, I am extracting HTML and for each /img/image1.png I find I want to append a url to it.
However, there are images that are already like that for example www.anylink.com/img/image2.png which do not need appending but are in the string in which I pulled. I looked at replaceAll() method but not sure if this allows for appending in replacement and also not sure if I need regex to search for instances where only /img/ exists(no url) and not the full url since only local hosted images I want to append to. I am looking for some suggestions as I am not sure how to begin this code after research.
Thank you.
I think that the method replaceAll() in String is enough for what you need.
You just need to write the correct regular expression.
If you write some examples, I can suggest the regex.
For example something like:
System.out.println("<div><img src=\"/test/this.png\" /></div>".replaceAll("src=\"/(.*)\"", "src=\"www.google.com$1\""));

Regex to dynamically ignore some parts of a given path

Consider that I have the following string which is a "complete path":
/A/B/C/D/E
And there is also the "simplified path" string:
/A/B/E
I have a situation where some parts of the string can be omitted and still be represent the full path. I know this is strange, but I can't change it.
Basically for this case, I need a regex to ignore the last two paths before the current path (dynamically as I have no specific information of them), to confirm that these two strings have a correlation.
The only thing I could came up with was:
Take the current path (([^\/]+$)) from both strings and compare.
Check in Java if the complete string contains the simplified one.
But I think there must be a cleaner way to do this.
I came up with the following solution:
Search string:
[^\/]+\/[^\/]+\/([^\/]+$)
Replace string: \1
Check it here
If both path point to the same file/directory then you could make use of the Files class.
It has a method Files#isSameFile to which you pass two Path instances and it would check if both files are pointing to the same file at your directory. This simple line would check if A/B/E/ and /A/B/C/D/E are actually the same directory.
System.out.println(Files.isSameFile(Paths.get("/A/B/C/D/E"), Paths.get("/A/B/E")));

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)

Can't implement RegEx on .dat file

I have to read through a .dat file with restaurant names, addresses, ratings etc. and display anything that isn't formatted correctly. The problem is not with the regular expression.
My problem is that I have no idea how to implement the regular expression so that it can read through the files and pick out any errors in the formatting of the above categories.
The contents of the file are not evenly spaced out so I can't just make a constructor that reads each substring. Is there any way I can use regular expressions to pull out the information I need from the file? Any help will be appreciated.
If you already have a regular expression, you can just test every line and print it, if it does not match.

Categories