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.
Related
I am using Java 8. I have a scenario where user can upload a document and I have to compare if the uploaded path contains the below path format:
"/abc:doc_home{anyWord}/xyz:docFolder{anyWord}/[someWord]/def:library{anyWord}"
I need the curly braces where I have indicated above and within that any word can be included. Is it possible to do this in regex?
You have included anyWord twice in the expression. If you intended for these two words to be the same word, then no, that is not possible to do with regular expressions.
I have a yaml which looks like this..! Sonar by default providing sonar-yaml-plugin with some templates which accepts regex as input to verify particular key is present or not in .yml file.
I want regex to match entire key logging:file
server:
port: 8989
logging:
file: ./sample1.txt
path: ./log
I have tried using (logging)(?s:.*?)(file) but its not validating when I use it in sonar-plugin.
I'm not so sure what you may want to match, but maybe this regex might help you to do so or design your desired expression:
^(logging:|\s+file:)(.+)
This expression has a left boundary on start ^.
Your two words connected with an OR (|)
Then, matches everything after that using .+
You can also add additional boundaries to it, however if you could add some real samples to your question, it would be easier to answer.
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,
To validate String in Java I can use String.matches(). I would like to validate a simple string "*.txt" where "*" means anything. Input e.g. test.txt is correct, but test.tt is not correct, because of ".tt". I tried to use matches("[*].txt"), but it doesn't work. How can I improve this matches? Thanks.
Do not use code, you don't understand!
For your simple problem you could totally avoid using a regular expression and just use
yourString.endsWith(".txt")
and if you want to perform this comparison case insensitive (i.e. allow ".TXT" or ".tXt") use
yourString.toLowerCase().endsWith(".txt")
If you want to learn more about regular expressions in java, I'd recomment a tutorial. For example this one.
You may try this for txt files:
"file.txt".matches("^.*[.]txt$")
Basically ^ means the start of your string. .* means match anything greedy, hence as much as you can get to make the expression match. And [.] means match the dot character. The suffix txt is just the txt text itself. And finally $ is the anchor for the end of the string, which ensures that the string does not contain anything more.
Use .+, it means any character having one or unlimited lengths. It will ensure to avoid the inputs like only .txt
matches(".+[.]txt")
FYI: [.] simply matches with the dot character.
I am looking for regular expression to test for files with more than one extensions, i.e test.1.log.old
Thanks,
M.
If you are only testing filenames, then this will work.
(.[^.]+\.){2}
You may need to modify the regex if you try to match against more than file names (path).