I'm trying to get a valid regex for password using Xeger or Generex by this code:
import com.mifmif.common.regex.Generex;
import nl.flotsam.xeger.Xeger;
public class main {
public static void main(String[] args) {
String regex = "((?=.*[a-z])(?=.*\\d)(?=.*[A-Z])(?=.*[\\p{Punct}]).{8,12})";
Xeger generator = new Xeger(regex);
Generex generator1 = new Generex(regex);
System.out.println(generator.generate());
System.out.println(generator1.random());
}
}
But what I got in the console is 2 very long strings (much more than 12 chars) and with some very strange chars.
Example for such string:
?=??JY??p? x??x??f????v????c4\??6??'??y4?Xoq?d??tT??o??tH?r^c??l??v?l$????*n xF?+?:y??^?"?dD&o#????????p'f???s?lx[j?iW???^??=?????u??x???=??A??a??)KNyu?ֽSUu?c?wA??jrn???qEi????s??e?Ybp?oj???N#d????\iH?V?u
??c?eM?Y9???kF????b?]rco?gB??L?E?J?\(y1????d?wgg?Mc???D?e?c?o?C?j?w???w??kr?a?????y#VxDi???hCz?j??p?t????h??d5f??L??S??i?Y??z&?#w?X??<os)sF?o??]??M??????k?='????R?V?dPH#CWBV)$d?d?_?d`?%??????Y^??IEK??2?=????dK??d)?8Td]d?=UGK?! ????X#J?X&?MX?={nptrcT]kpp?^op|?o{pPpo
How can I create a valid string?
Thanks.
Related
Hey I am trying to replace the a regex pattern in a directory of files and replace with this character 'X'. I started out trying alter one file but that is not working. I cam eup with the following code any help would be appreciated.
My goal is to read all the file content find the regex pattern and replace it.
Also this code is not working it runs but dose nothing to the text file.
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
public class DataChange {
public static void main(String[] args) throws IOException {
String absolutePathOne = "C:\\Users\\hoflerj\\Desktop\\After\\test.txt";
String[] files = { "test.txt" };
for (String file : files) {
File f = new File(file);
String content = FileUtils.readFileToString(new File(absolutePathOne));
FileUtils.writeStringToFile(f, content.replaceAll("2018(.+)", "X"));
}
}
}
File Content inside the file is:
3-MAAAA2017/2/00346
I am trying to have it read through and replace 2017/2/00346 with XXX's
my goal is to do this for like 3 files at one time also.
Im trying to build a categorizer in version 1.8 of openNLP but with the code below I keep getting a NullPointerException. What am I doing wrong?
public class test
{
public static void main(String[] args) throws IOException
{
InputStream is = new FileInputStream("D:/training.txt");
DoccatModel m = new DoccatModel(is);
Tokenizer tokenizer = WhitespaceTokenizer.INSTANCE;
String tweet = "testing sentence";
String[] tokens = tokenizer.tokenize(tweet);
DocumentCategorizerME myCategorizer = new DocumentCategorizerME(m);
double[] outcomes = myCategorizer.categorize(tokens);
String category = myCategorizer.getBestCategory(outcomes);
}
}
You should have a look at following tutorial. They are useing OpenNLP version 1.7.2. This may be a more recent example to work with.
https://www.tutorialkart.com/opennlp/training-of-document-categorizer-using-naive-bayes-algorithm-in-opennlp/
Hope it helps.
Consider the source_folder_name as D:\Desktop\test util\config sql
In the code given below the white space in the folder name i.e test util and config sql is not handled properly.
I need to escape those white spaces.
public class SqlToTblCoverter {
private File source_folder = null;
public SqlToTblCoverter(String source_folder_name) {
source_folder = new File(source_folder_name);
}
public void check() {
System.out.println("Source folder"+ source_folder);
}
}
public class test{
public static void main(String args[]){
Scanner in=new Scanner(System.in);
System.out.println("Enter Input Folder Path");
String input=in.next();
SqlToTblCoverter config_migrator = new
SqlToTblCoverter(input);
}
}
On executing the above code the value of source_folder comes is
Source folder: D:\Desktop\test
whereas expected is
Source folder: D:\Desktop\test util\config sql
Please help me out
If you read the documentation of Scanner you will see:
A Scanner breaks its input into tokens using a delimiter pattern,
which by default matches whitespace.
So the problem is your scanner. If you use the newlines as delimiter, everything should work as expected.
The following java code:
import java.io.File;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
class extract
{
public static void main (String[] args) throws java.lang.Exception
{
String testData = new Scanner( new File("109.txt") ).useDelimiter("\\A").next();
//String testData = "#1|77|1391436891|1|1|00:1e:58:f4:15:f7|Nexus 4, 4.4, MAKOZ30d $1|1391436893 ?[176.08179, -13.839829, -1.0054213] %PKKV7|00:7f:28:3f:17:9d|-67|2437 %DC2VJ|f8:e4:fb:a0:06:f8|-71|2412 %VVWSP|00:7f:28:d5:92:65|-71|2462 %SVT8H|f8:e4:fb:8e:d6:9b|-77|2437 %ThreeBestFriends|20:10:7a:14:6a:f7|-66|2452 %2X4C8|00:7f:28:44:23:da|-75|2437 %STDGD|f8:e4:fb:70:86:f4|-82|2462 %DeathStar|00:7f:28:be:c8:94|-84|2412 %Freeinternet|00:1e:58:f4:15:f7|-59|2437 %QB657|00:26:62:b7:16:4b|-88|2462 %375F2|00:26:b8:3e:0a:14|-70|2412 %E1K38|00:26:62:cf:90:37|-81|2412";
String regularExpression = "\\w{2}:\\w{2}:\\w{2}:\\w{2}:\\w{2}:\\w{2}\\W{1}-\\d{2}";
Pattern pattern = Pattern.compile(regularExpression);
Matcher matcher = pattern.matcher(testData);
while(matcher.find()) {
System.out.println(matcher.group(0));
}
}
}
generates the following output:
00:1a:1e:87:04:42|-87
00:1a:1e:8e:e9:a2|-77
00:1a:1e:87:04:51|-95
00:1a:1e:84:92:02|-84
00:1a:1e:8d:f7:a2|-67
00:1a:1e:82:b8:e1|-56
00:1a:1e:82:b8:e2|-54
00:1a:1e:82:b8:e0|-56
00:1a:1e:87:04:41|-88
00:1a:1e:8d:f7:b1|-78
00:1a:1e:8d:f7:b2|-78
I'm trying to save output file as an excel file separated by columns.
Does anybody has any suggestions on how to achieve this?
Thank you!
Just add some commas (currently you don't have any), and save it as a text file named some_file_name.csv. You may use e.g. a BufferedWriter for this.
Saying 'Excel file separated by commas' is incorrect, actually the proper term is 'comma separated values file' or 'CSV file'. Programs other than Excel can open such files too.
considering the following file "sample.txt" as input for a java program which contains the following information:
#1|77|1391436891|1|1|00:1e:58:f4:15:f7|Nexus 4, 4.4, MAKOZ30d
$1|1391436893
?[176.08179, -13.839829, -1.0054213]
%PKKV7|00:7f:28:3f:17:9d|-67|2437
%DC2VJ|f8:e4:fb:a0:06:f8|-71|2412
%VVWSP|00:7f:28:d5:92:65|-71|2462
%SVT8H|f8:e4:fb:8e:d6:9b|-77|2437
%ThreeBestFriends|20:10:7a:14:6a:f7|-66|2452
%2X4C8|00:7f:28:44:23:da|-75|2437
%STDGD|f8:e4:fb:70:86:f4|-82|2462
%DeathStar|00:7f:28:be:c8:94|-84|2412
%Freeinternet|00:1e:58:f4:15:f7|-59|2437
%QB657|00:26:62:b7:16:4b|-88|2462
%375F2|00:26:b8:3e:0a:14|-70|2412
%E1K38|00:26:62:cf:90:37|-81|2412
I'm trying to get an "output.txt" file as following:
00:7f:28:3f:17:9d|-67
f8:e4:fb:a0:06:f8|-71
00:7f:28:d5:92:65|-71
f8:e4:fb:8e:d6:9b|-77
20:10:7a:14:6a:f7|-66
00:7f:28:44:23:da|-75
f8:e4:fb:70:86:f4|-82
00:7f:28:be:c8:94|-84
00:1e:58:f4:15:f7|-59
00:26:62:b7:16:4b|-88
00:26:b8:3e:0a:14|-70
00:26:62:cf:90:37|-81
any recommendations on how to achieve this in java?
The following regular expression will extract this:
\w{2}:\w{2}:\w{2}:\w{2}:\w{2}:\w{2}\W{1}-\d{2}
http://ideone.com/FwpV7z
import java.util.*;
import java.lang.*;
import java.io.*;
import java.util.regex.*;
class Ideone
{
public static void main (String[] args) throws java.lang.Exception
{
String testData = "#1|77|1391436891|1|1|00:1e:58:f4:15:f7|Nexus 4, 4.4, MAKOZ30d $1|1391436893 ?[176.08179, -13.839829, -1.0054213] %PKKV7|00:7f:28:3f:17:9d|-67|2437 %DC2VJ|f8:e4:fb:a0:06:f8|-71|2412 %VVWSP|00:7f:28:d5:92:65|-71|2462 %SVT8H|f8:e4:fb:8e:d6:9b|-77|2437 %ThreeBestFriends|20:10:7a:14:6a:f7|-66|2452 %2X4C8|00:7f:28:44:23:da|-75|2437 %STDGD|f8:e4:fb:70:86:f4|-82|2462 %DeathStar|00:7f:28:be:c8:94|-84|2412 %Freeinternet|00:1e:58:f4:15:f7|-59|2437 %QB657|00:26:62:b7:16:4b|-88|2462 %375F2|00:26:b8:3e:0a:14|-70|2412 %E1K38|00:26:62:cf:90:37|-81|2412";
String regularExpression = "\\w{2}:\\w{2}:\\w{2}:\\w{2}:\\w{2}:\\w{2}\\W{1}-\\d{2}";
Pattern pattern = Pattern.compile(regularExpression);
Matcher matcher = pattern.matcher(testData);
while(matcher.find()) {
System.out.println(matcher.group(0));
}
}
}
output:
00:7f:28:3f:17:9d|-67
f8:e4:fb:a0:06:f8|-71
00:7f:28:d5:92:65|-71
f8:e4:fb:8e:d6:9b|-77
20:10:7a:14:6a:f7|-66
00:7f:28:44:23:da|-75
f8:e4:fb:70:86:f4|-82
If you read the line as a String you can use split to split it up for the pipes.
Here's a good start (might be typo's since I did write it straight in here)
//Read all lines, one line at a time with a simple scanner:
Scanner sc = new Scanner(new File("sample.txt"));
while (sc.hasNextLine()){
String line = sc.nextLine();
//If the line starts with a % do something, else ignore
if (line.charAt(0) == '%'){
int firstPipe = line.indexOf("|");
int secondPipe = line.indexOf("|",firstPipe);
//Now use String.substring() and perhaps a temporary StringBuffer storage followed by, for example, a FileWriter to create the output file