Can I use regex in properties file in JAVA? - java

I am trying to assert some text that is stored in properties file:
Text=\
some text\n\
to be\n\
asserted\n\
based on 1567 ratings
1567 vary so I want to make sure that it get's replaced with regex that will say that this can be any number using \d or \d, since I have to escape \ in properties file.
So I tried using
Text=\
some text\n\
to be\n\
asserted\n\
based on \\d ratings
this is the method I am using to assert:
Assert.assertEquals(PropertyLoader.loadProperty("filename.properties", "Text"),actual);
actual is a WebElement that gives me the text from website, I use actual.getText()
This is the class that has a loadProperty method
class PropertyLoader {
static String loadProperty(String file, String name) {
Properties props = new Properties();
try {
props.load(getResourceAsStream(file));
} catch (IOException e) {
e.printStackTrace();
}
return props.getProperty(name);
}
}
End result is that i am getting
Comparison Failure:
Expected:
some text
to be
asserted
based on \d ratings
Actual:
some text
to be
asserted
based on 1567 ratings
Not sure if this is even possible or I am simply missing something?

First the content of your properties file should be:
Text=\
some text\n\
to be\n\
asserted\n\
based on \\d+ ratings
Then your test case will be:
Assert.assertTrue(
Pattern.compile(
PropertyLoader.loadProperty("filename.properties", "Text"),
Pattern.MULTILINE
).matcher(actual).matches()
);

Related

using Regex pattern match for pipe delimited string

I am trying to do pattern match on pipe delimited string in java . But not sure what's going wrong. Need help from experts.
A|Bill Access Key|CBEBALCOM|
D|215325775|20210507|9|BALCOM SYSTEMS LTD|||
I|1|Back of Duplex Page|
I have file with records as above and I want to find record starting with 'D' and get the next pipe delimited values. And store those in some POJO object.
So tried to first read values by applying pattern but unable to find match.
String pattern = "r'D((?:\"(.*?)\"))'";
Pattern r = Pattern.compile(pattern); Matcher m = r.matcher(line);
if (m.find( )) {
System.out.println("value1" + m.group(0) );
System.out.println("value2" + m.group(1) );
} else {
System.out.println("No Match");
}
You might try it like this rather than using a pure regex solution
stream the lines of the file
filter for lines starting with D|
split filtered lines starting at index 2
create new array of just first two values
and collect in a List<String[]>
List<String[]> list = Files.lines(Path.of("Myfile.txt"))
.filter(str->str.startsWith("D|"))
.map(str->str.substring(2).split("\\|",3))
.map(arr->new String[]{arr[0],arr[1]})
.collect(Collectors.toList());
} catch (IOException ioe) {
ioe.printStackTrace();
}

Space in properties file [duplicate]

I am trying to load all the property names present in the properties file using the below code:
for(Enumeration<String> en = (Enumeration<String>) prop.propertyNames();en.hasMoreElements();){
String key = (String)en.nextElement();
System.out.println("Property name is "+key);
}
But my properties file has the below contents:
username=
password=
Parent file name=
Child file name =
After running the code I am getting output as :
username password Parent Child
If the property name has spaces, it is only returning the first word..
Can any one please tell me how to do this?
You can escape the spaces in your properties file, but I think it will start to look pretty ugly.
username=a
password=b
Parent\ file\ name=c
Child\ file\ name=d
You might be better of writing your own implementation with split() or indexOf() or whatever your heart desires to avoid any future bugs and/or headaches.
In Java.util.Properties , =, :, or white space character are key/value delimiter when load from property file.
Below are detailed Javadoc of its public void load(Reader reader)
The key contains all of the characters in the line starting with the first non-white space character and up to, but not including, the first unescaped =, :, or white space character other than a line terminator. All of these key termination characters may be included in the key by escaping them with a preceding backslash character. http://docs.oracle.com/javase/6/docs/api/
This is how I do it:
public class PropHelper {
final static String PROPERTY_FILEPATH = "blah/blah.properties";
static String getPropertyWithSpaces(String property, String delimiter) {
try {
FileReader reader = new FileReader(PROPERTY_FILEPATH);
Properties propertiesObj = new Properties();
propertiesObj.load(reader);
return propertiesObj.getProperty(property).replaceAll(delimiter, "");
} catch (Exception ex) {
System.out.println("FATAL ERROR: " + ex.getMessage());
System.exit(1);
}
return null;
}
}
Somewhere in .properties file:
settings = ` ⚙ Settings `
This is how I call it:
System.out.println("|" + PropHelper.getPropertyWithSpaces("settings", "`") + "|");
This method works with leading, internal and trailing spaces.
Enjoy!
It seems to be working fine for me; here is my code:
Properties prop = new Properties();
prop.setProperty("test1", "val1");
prop.setProperty("test number 2", "val number 2");
prop.setProperty("test 3", "val3");
prop.setProperty("test #4", "val #4");
for(Enumeration<String> en = (Enumeration<String>) prop.propertyNames();en.hasMoreElements();){
String key = (String)en.nextElement();
System.out.println("'" + key + "'='" + prop.getProperty(key) + "'");
}
And the output:
'test 3'='val3'
'test number 2'='val number 2'
'test1'='val1'
'test #4'='val #4'
You can compare that to yours as far as setting it goes, as our displaying seems to be the same. If you don't see anything, add your full code, and I'll take a look

Cleaning a file name in Java

I want to write a script that will clean my .mp3 files.
I was able to write a few line that change the name but I want to write an automatic script that will erase all the undesired characters $%_!?7 and etc. while changing the name in the next format Artist space dash Song.
File file = new File("C://Users//nikita//Desktop//$%#Artis8t_-_35&Son5g.mp3");
String Original = file.toString();
String New = "Code to change 'Original' to 'Artist - Song'";
File file2 = new File("C://Users//nikita//Desktop//" + New + ".mp3");
file.renameTo(file2);
I feel like I should make a list with all possible characters and then run the String through this list and erase all of the listed characters but I am not sure how to do it.
String test = "$%$#Arti56st_-_54^So65ng.mp3";
Edit 1:
When I try using the method remove, it still doesn't change the name.
String test = "$%$#Arti56st_-_54^So65ng.mp3";
System.out.println("Original: " + test);
test.replace( "[0-9]%#&\\$", "");
System.out.println("New: " + test);
The code above returns the following output
Original: $%$#Arti56st_-_54^So65ng.mp3
New: $%$#Arti56st_-_54^So65ng.mp3
I'd suggest something like this:
public static String santizeFilename(String original){
Pattern p = Pattern.compile("(.*)-(.*)\\.mp3");
Matcher m = p.matcher(original);
if (m.matches()){
String artist = m.group(1).replaceAll("[^a-zA-Z ]", "");
String song = m.group(2).replaceAll("[^a-zA-Z ]", "");
return String.format("%s - %s", artist, song);
}
else {
throw new IllegalArgumentException("Failed to match filename : "+original);
}
}
(Edit - changed whitelist regex to exclude digits and underscores)
Two points in particular - when sanitizing strings, it's a good idea to whitelist permitted characters, rather than blacklisting the ones you want to exclude, so you won't be surprised by edge cases later. (You may want a less restrictive whitelist than I've used here, but it's easy to vary)
It's also a good idea to handle the case that the filename doesn't match the expected pattern. If your code comes across something other than an MP3, how would you like it to respond? Here I've through an exception, so the calling code can catch and handle that appropriately.
String new = original.replace( "[0-9]%#&\\$", "")
this should replace almost all the characters you don't want
or you can come up with your own regex
https://docs.oracle.com/javase/tutorial/essential/regex/

How to detect URL to different page (also in the same domain)

I have question about detect url in page. I'm founding the best way how it solve. For downloading page I use Jsoup.
URI uri = new URI("http://www.niocchi.com/");
Document doc = Jsoup.connect(uri.toString()).get();
Elements links = doc.select("a")
And this page get me some links. For example this:
http://www.niocchi.com/#Package organization
http://www.niocchi.com/#Architecture
http://www.linkedin.com/in/ivanprado
http://www.niocchi.com/examples/
I need get only different pages without references to paragraphs.
I would like to get from example this:
http://www.linkedin.com/in/ivanprado
http://www.niocchi.com/examples/
It looks like you want to select only these <a> with href attribute with value build from characters which are not #. In that case you can use
doc.select("a[href~=^[^#]+$]")
attribute~=regex is syntax used to check if part of value of attribute can be matched with regex.
regex accepting one or more non # characters can look like this [^#]+
regex accepting only entire string (not only its part) need to be surrounded with ^ and $ anchors which represents
^ - start of the string,
$ end of the string.
You could convert them to strings and then split them based on the # mark.
for example:
public void stringSplitter() {
String result = null;
// example
String[] stringURL = {"http://www.niocchi.com/#Package organization", "http://www.niocchi.com/#Architecture",
"http://www.linkedin.com/in/ivanprado", "http://www.niocchi.com/examples/ "};
try {
for (int i = 0; i < stringURL.length; i++) {
String [] parts = stringURL[i].split("#");
result = parts[0];
System.out.println(result);
}
}catch (Exception ex) {
ex.printStackTrace();
}
}
The output is:
http://www.niocchi.com/
http://www.niocchi.com/
http://www.linkedin.com/in/ivanprado
http://www.niocchi.com/examples/
I would even think about setting a part of the method to return only unique URL's

LucidWorks: Java Regular Expressions & GNU Regular Expressions

I am trying to create regular expressions so that I can crawl and index certain URL's on my web site with LucidWorks.
Example URL: http://www.example.com/reviews/assassins-creed-revelations/24475/reviews/
Example URL: http://www.example.com/reviews/super-mario-3d-land/64303/reviews/
Basically, I want LucidWorks to search my entire site and index only URL'S that have /reviews/ at the end of the URL.
Could anyone help me construct an expression to do that please? :)
Updated:
URL: http://www.example.com/
Include paths: //*/reviews/*
That kind of worked, but it only crawls the first page, it won't go to the next page with more reviews (1,2,3 etc).
If I also add: ///reviews/.*
I get a load of pages indexed which I don't want such as http://www.example.com/?page=2
Check with this function
public boolean canAcceptURL(String url,String endsWith){
boolean canAccept = false;
String regex = "";
try{
if(endsWith.equals("")){
endsWith = "/reviews/";
}
regex = "[\\x20-\\x7E]*"+endsWith+"$";//Check the url string u passed ends with the endString you hav passed.If end string is null it will take the default value.
canAccept = url.matches(regex);
}catch (PatternSyntaxException pe) {
pe.printStackTrace();
}catch (Exception e) {
e.printStackTrace();
}
System.out.println("String matches : "+canAccept);
return canAccept;
}
Sample out put :
calling function : canAcceptURL("http://www.example.com/reviews/super-mario-3d-land/64303/reviews/","/reviews/");
String matches : true
if you want to get the url contains *'/reviews/'* just change the regex string to
String regex = "[\\x20-\\x7E]*/reviews/[\\x20-\\x7E]*"; // this will accept a string with white space and special character.

Categories

Resources