Java Matcher class with regex and loop - java

String sCurrentLine;
br = new BufferedReader(new FileReader(path));
while ((sCurrentLine = br.readLine()) != null) {
Pattern pattern = Pattern.compile(".*?unregistKey\\(tvKey\\.(.*?)\\);");
Matcher m= pattern.matcher(sCurrentLine);
if(m.matches()) {
String abc = m.group(1) ;
System.out.println ("aaaaaaaaaaaaaa" + abc.toString());
}
}
Why this code is looping more than 1 time.
I checked this call to this code but its coming only 1 time.
o/p is N times like this:
aaaaaaaaaaaaaaKEY_1
aaaaaaaaaaaaaaKEY_2
aaaaaaaaaaaaaaKEY_3
aaaaaaaaaaaaaaKEY_CH_UP
aaaaaaaaaaaaaaKEY_PANEL_CH_UP
aaaaaaaaaaaaaaKEY_CH_DOWN
aaaaaaaaaaaaaaKEY_1
aaaaaaaaaaaaaaKEY_2
aaaaaaaaaaaaaaKEY_3
aaaaaaaaaaaaaaKEY_CH_UP
aaaaaaaaaaaaaaKEY_PANEL_CH_UP
aaaaaaaaaaaaaaKEY_CH_DOWN

You will see this output only when the input file contains the same pattern several times (i.e. there are several lines that contain KEY_1, etc).

Related

Generate the unique pattern for the given 4 cases using pattern regx in java

Here is my input data(as a text file):
Stream: (4040) "StreamTestDeleteit"
Stream: (4037) "RBL_RB_REF_VMPSufgf_Int" <-> (4009) "RBL_DAI_BR222313_VMPS_Android"
Stream: (4002) "HPNEtd_Team23_dep_20190616"
Stream: (4002) "HPNE-td_Team23_dep_20190616fg"
Output i am excepting is :
StreamTestDeleteit
RBL_RB_REF_VMPSufgf_Int
HPNEtd_Team23_dep_20190616
HPNE-td_Team23_dep_20190616fg
Output i am getting :
StreamTestDeleteit
RBL_DAI_BR222313_VMPS_Android
HPNEtd_Team23_dep_20190616
HPNE-td_Team23_dep_20190616fg
so what i want is need one pattern which will work for all the 4 cases
My Snippet:
FileInputStream fstream = new FileInputStream(filepath);
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
while ((strLine = br.readLine()) != null)
{
Pattern p = Pattern.compile("Stream:(.*) \"(.*)\"");
Matcher m = p.matcher(strLine);
while (m.find())
{
streamInp = m.group(2);
streamMatcher.add(streamInp);
}
}
br.close();
Changed the regex as previously given in a comment, and it worked fine with the code given in the question. See below.
I do however also have a few comments on the code:
Don't include a DataInputStream in the stream pipeline.
Use the newer Java 7+ NIO.2 methods for reading files.
Use the Java 7+ try-with-resources statement to manage the closing of the file stream.
Updated code with working regex:
try (BufferedReader br = Files.newBufferedReader(Paths.get(filepath))) {
for (String strLine; (strLine = br.readLine()) != null; ) {
Pattern p = Pattern.compile("Stream:.*? \"(.*?)\"");
for (Matcher m = p.matcher(strLine); m.find(); ) {
String streamInp = m.group(1);
System.out.println(streamInp);
}
}
}
Output
StreamTestDeleteit
RBL_RB_REF_VMPSufgf_Int
HPNEtd_Team23_dep_20190616
HPNE-td_Team23_dep_20190616fg

Reading a set of values from a text file

{
"TEST":"189456",
"TEST1":"X_Y_Z",
"TEST2":"Y_Z_W",
"TEST3":"GGG ",
"TEST4":"32423423233322"
},
{
"TEST":"123456",
"TEST1":"X_E_Z",
"TEST2":"T_Z_W",
"TEST3":"EWE ",
"TEST4":"324234243234"
}
This is a .txt file I want to read and print only 189456,123456 from the above file.Can anyone help me in doing this.Please find the code for reference.Please post the easiest code.....
Pattern p = Pattern.compile("\"Test\"\\s*:\\s*\"(.*)\"", Pattern.CASE_INSENSITIVE);
while ( (line = bf.readLine()) != null) {
linecount++;
Matcher m = p.matcher(line);
// indicate all matches on the line
while (m.find()) {
System.out.println(m.group(1));
}
}
Another way to do it:
while ((line = br.readLine()) != null) {
if(line.contains("\"TEST:\"")){
String[] lineValues = line.split(":");
System.out.println(lineValues[1].replace("\"", "").replace(",",""));
}
}
As for a Regex solution :
(.*)\"TEST":\"(.*?)\"
Note the ? , it makes your regex to stop at the first match of ".
With spaces in between :
(.*)\"TEST"\s*:\s*\"(.*?)\"
With provided input, you should read it as json instead of raw text.
com.fasterxml.jackson.databind.ObjectMapper mapper = new com.fasterxml.jackson.databind.ObjectMapper();
List<TestObj> test = new ArrayList<TestObj>();
test = mapper.readValue(new File("c:\\YourFile.txt"), test.getClass());
Where TestObj is something like this:
class TestObj {
String test;
String test1; // You should use json annotation here because it does not match your json field name.
...
// getter setter methods
}
Hope I understood the question the right way :D
String saveData;
Pattern p = Pattern.compile("\"Test\"\\s*:\\s*\"(.*)\"", Pattern.CASE_INSENSITIVE);
while ( (line = bf.readLine()) != null) {
linecount++;
Matcher m = p.matcher(line);
// indicate all matches on the line
if(line.contains("189456") || line.contains("123456")) {
saveData = line;
}
}
if the String you get from readLine() contains the searched string it will save it in saveData
FileInputStream fstream = new FileInputStream("D:\\prac\\src\\test.txt");
BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
String strLine;
while ((strLine = br.readLine()) != null) {
if(strLine.contains("\"TEST\":")){
System.out.println(strLine.split(":")[1].replaceAll("\"","").replace(",",""));
}
}
br.close();
}
Output:
189456
123456

Regex pattern to find Integers in every line of the string

I have a pattern here which finds the integers after a comma.
The problem I have is that my return value is in new lines, so the pattern only works on the new line. How do I fix this? I want it to find the pattern in every line.
All help is appreciated:
url = new URL("https://test.com");
con = url.openConnection();
is = con.getInputStream();
br = new BufferedReader(new InputStreamReader(is));
while ((line = br.readLine()) != null) {
String responseData = line;
System.out.println(responseData);
}
pattern = "(?<=,)\\d+";
pr = Pattern.compile(pattern);
match = pr.matcher(responseData); // String responseData
System.out.println();
while (match.find()) {
System.out.println("Found: " + match.group());
}
Here is the response returned as a string:
test.test.test.test.test-test,0,0,0
test.test.test.test.test-test,2,0,0
test.test.test.test.test-test,0,0,3
Here is the printout:
Found: 0
Found: 0
Found: 0
The problem is with building your String, you're assigning only the last line from the BufferedReader:
responseData = line;
If you print responseData before you try to match, you'll see it's only one line, and not what you expected.
Since you're printing the buffer's content using a System.out.println, you do see the whole result, but what's getting saved to responseData is actually the last line.
You should use a StringBuilder to build the whole string:
StringBuilder str = new StringBuilder();
while ((line = br.readLine()) != null) {
str.append(line);
}
responseData = str.toString();
// now responseData contains the whole String, as you expected
Tip: Use the debugger, it'll make you better understand your code and will help you to find bugs very faster.
You can use the Pattern.MULTILINE option when compiling your regex:
pattern = "(?<=,)\\d+";
pr = Pattern.compile(pattern, Pattern.MULTILINE);

How to apply regex to entire file, not just line after line?

I want to apply my regular expression not just to the first line of the text file, but to the all lines together.
Currently it matches only when the entire appropriate match is on one line. And if the appropriate match continues on the next line - it doesn't match at all.
class Parser {
public static void main(String[] args) throws IOException {
Pattern patt = Pattern.compile("(include|"
+ "integrate|"
+ "driven based on|"
+ "facilitate through|"
+ "contain|"
+ "using|"
+ "equipped"
+ "integrate|"
+ "implement|"
+ "utilized to facilitate|"
+ "comprise){1}"
+ "[\\s\\w\\,\\(\\)\\;\\:]*\\."); //Regex
BufferedReader r = new BufferedReader(new FileReader("E:/test/test.txt")); // read the file
String line;
PrintWriter pWriter = null;
while ((line = r.readLine()) != null) {
Matcher matcher = patt.matcher(line);
while (matcher.find()) {
try{
pWriter = new PrintWriter(new BufferedWriter(new FileWriter("E:/test/test1.txt", true)));//append any given input
pWriter.println(matcher.group()); //write the result of matcher to the new file
} catch (IOException ioe) {
ioe.printStackTrace();
} finally {
if (pWriter != null){
pWriter.flush();
pWriter.close();
}
}
System.out.println(matcher.group());
}
}
}
}
Change while ((line = r.readLine()) != null) to this:
String file = ""; // Basically, a conglomerate of all of the lines in the file
while ((line = r.readLine()) != null) {
file += line; // Append each line to the "file" string
}
Matcher matcher = patt.matcher(file);
while (matcher.find()) {
/* Blah blah blah, your outputting goes here. */
}
The reason why this happens is because you're doing each line individually. For what you want, you need to apply the regex to the file all at once.
Currently the matcher is applied per line, it needs to be applied to the whole file to work as intended.
Regex are greedy, you will match the whole String on the first match unless you have . (or other special characters) in your String:
...
+ "comprise){1}"
+ "[\\s\\w\\,\\(\\)\\;\\:]*\\."); //Regex
On the last line you match any whitespace and word, so pretty much anything but .. Also the {1} and most of the \ are superfluous (because in []):
...
+ "comprise)"
+ "[\\s\\w,();:]*\\."); //Regex
If you don't care about the newline characters just remove them first and it should work (I see no way around it if you have something like "com\nprise" and want to match that):
s = s.replaceAll("\\n+", "");

Java String Matching in a Sorted File and grouping similar data

i have sorted file and i need to do the following pattern match. I read the row and then compare or do patern match with the row just after it , if it matches then insert the string i used to match after a comma in that row and move on to the next row. I am new to Java and overwhelmed with options from Open CSV to BufferedReader. I intend to iterate through the file till it reaches the end. I may always have blanks and have a dated in quotes. The file size would be around 100 MBs.
My file has data like
ABCD
ABCD123
ABCD456, 123
XYZ
XYZ890
XYZ123, 890
and output is expected as
ABCD, ABCD
ABCD123, ABCD
ABCD456, 123, ABCD
XYZ, XYZ
XYZ890, XYZ
XYZ123, 890, XYZ
Not sure about the best method. Can you please help me.
To open a file, you can use File and FileReader classes:
File csvFile = new File("file.csv");
FileReader fileReader = null;
try {
fileReader = new FileReader(csvFile);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
You can get a line of the file using Scanner:
Scanner reader = new Scanner(fileReader);
while(reader.hasNext()){
String line = reader.nextLine();
parseLine(line);
}
You want to parse this line. For it, you have to study Regex for using Pattern and Matcher classes:
private void parseLine(String line) {
Matcher matcher = Pattern.compile("(ABCD)").matcher(line);
if(matcher.find()){
System.out.println("find: " + matcher.group());
}
}
To find the next pattern of the same row, you can reuse matcher.find(). If some result was found, it will return true and you can get this result with matcher.groud();
Read line by line and use regex to replace it as per your need using String.replaceAll()
^([A-Z]+)([0-9]*)(, [0-9]+)?$
Replacement : $1$2$3, $1
Here is Online demo
Read more about Java Pattern
Sample code:
String regex = "^([A-Z]+)([0-9]*)(, [0-9]+)?$";
String replacement = "$1$2$3, $1";
String newLine = line.replaceAll(regex,replacement);
For better performance, read 100 or more lines at a time and store in a buffer and finally call String#replaceAll() single time to replace all at a time.
sample code:
String regex = "([A-Z]+)([0-9]*)(, [0-9]+)?(\r?\n|$)";
String replacement = "$1$2$3, $1$4";
StringBuilder builder = new StringBuilder();
int counter = 0;
String line = null;
try (BufferedReader reader = new BufferedReader(new FileReader("abc.csv"))) {
while ((line = reader.readLine()) != null) {
builder.append(line).append(System.lineSeparator());
if (counter++ % 100 == 0) { // 100 lines
String newLine = builder.toString().replaceAll(regex, replacement);
System.out.print(newLine);
builder.setLength(0); // reset the buffer
}
}
}
if (builder.length() > 0) {
String newLine = builder.toString().replaceAll(regex, replacement);
System.out.print(newLine);
}
Read more about Java 7 - The try-with-resources Statement

Categories