{
"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
Related
I am trying to remove a specific lines in a text file using regex but I am receiving an Illegal State Exception. I am recently trying to get accustomed to regex and have tried to to use match.matches(); but that solution has not worked for me . any advice to what I am doing wrong
try {
BufferedReader br = new BufferedReader(new FileReader("TestFile.txt"));
//System.out.println(br.toString());
ArrayList<String> list = new ArrayList<String>();
String line= br.readLine() ;
while (br.readLine() != null ) {
//System.out.println(line);
//System.out.println("test1"); {
Pattern regex = Pattern.compile("[^\\s\"]+|\"[^\"]*\"");
Matcher regexMatcher = regex.matcher(line);
String match = regexMatcher.group();// here is where the illegalstateexception occurs
match = removeLeadingChar(match, "\"");
match = removeLeadingChar(match, "\"");
list.add(match);
// }
// br.close();
System.out.println(br);
Exception in thread "main" java.lang.IllegalStateException: No match found
at java.base/java.util.regex.Matcher.group(Unknown Source)
at java.base/java.util.regex.Matcher.group(Unknown Source)
Use Matcher.find() method to see if there is a match in the regular expression pattern. Debug the results of the regexMatcher.find() method in the IDE(e.g. IntelliJ)
try {
BufferedReader br = new BufferedReader(new FileReader("TestFile.txt"));
ArrayList<String> list = new ArrayList<>();
String line;
// Assign one line read from the file to a variable
while ((line = br.readLine()) != null) {
System.out.println(line);
Pattern regex = Pattern.compile("[^\\s\"]+|\"[^\"]*\"");
Matcher regexMatcher = regex.matcher(line);
// Returns true if a match is found for the regular expression pattern.
while (regexMatcher.find()) {
String match = regexMatcher.group();
match = removeLeadingChar(match, "\"");
match = removeLeadingChar(match, "\"");
list.add(match);
}
}
// What is the purpose of this code?
System.out.println(br);
// If you want to output the string elements of the list
System.out.println(list.toString());
// must be closed after use.(to prevent memory leak)
br.close();
} catch (IOException e) {
// exception handling
e.printStackTrace();
}
You had the while loop wrong so it causes the line to be null, try that:
try {
BufferedReader br = new BufferedReader(new FileReader("TestFile.txt"));
ArrayList<String> list = new ArrayList<String>();
String line; // <--- FIXED
while ((line = br.readLine()) != null) { // <--- FIXED
Pattern regex = Pattern.compile("[^\\s\"]+|\"[^\"]*\"");
Matcher regexMatcher = regex.matcher(line);
String match = regexMatcher.group();// here is where the illegalstateexception occurs
match = removeLeadingChar(match, "\"");
match = removeLeadingChar(match, "\"");
list.add(match);
}
br.close();
System.out.println(list.toString());
}
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);
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+", "");
I only want to read the first line of a text file and put that first line in a string array.
This is what I have but its reading the whole file.
ex text in myTextFile:
Header1,Header2,Header3,Header4,Header5
1,2,3,4,5
6,7,8,9,10
String line= System.getProperty("line.separator");
String strArray[] = new String[5];
String text = null;
BufferedReader brTest = new BufferedReader(new FileReader(myTextFile));
text = brTest .readLine();
while (text != line) {
System.out.println("text = " + text );
strArray= text.split(",");
}
use BufferedReader.readLine() to get the first line.
BufferedReader brTest = new BufferedReader(new FileReader(myTextFile));
text = brTest .readLine();
System.out.println("Firstline is : " + text);
If I understand you, then
String text = brTest.readLine();
// Stop. text is the first line.
System.out.println(text);
String[] strArray = text.split(",");
System.out.println(Arrays.toString(strArray));
With Java 8 and java.nio you can also do the following:
String myTextFile = "path/to/your/file.txt";
Path myPath = Paths.get(myTextFile);
String[] strArray = Files.lines(myPath)
.map(s -> s.split(","))
.findFirst()
.get();
If TAsks assumption is correct, you can realize that with an additional
.filter(s -> !s.equals(""))
Also, beside of all other solutions presented here, you could use guava utility class (Files), like below:
import com.google.common.io.Files;
//...
String firstLine = Files.asCharSource(myTextFile).readFirstLine();
I think you are trying to get one line only if it's not empty.
You can use
while ((text=brTest .readLine())!=null){
if(!text.equals("")){//Ommit Empty lines
System.out.println("text = " + text );
strArray= text.split(",");
System.out.println(Arrays.toString(strArray));
break;
}
}
Use this
BuffereedReader reader = new BufferedReader(new FileReader(textFile));
StringBuilder builder = new StringBuilder();
StringBuilder sb = new StringBuilder();
String line = br.readLine();
while (line != null) {
sb.append(line);
break;
}
if(sb.toString.trim().length!=0)
System.out.println("first line"+sb.toString);
I hope this will help someone
to read the first line:
public static String getFirstLine() throws IOException {
BufferedReader br = new BufferedReader(new FileReader("C:\\testing.txt"));
String line = br.readLine();
br.close();
return line;
}
to read the whole text:
public static String getText() throws IOException {
BufferedReader br = new BufferedReader(new FileReader("C:\\testing.txt"));
StringBuilder sb = new StringBuilder();
String line = br.readLine();
while (line != null) {
sb.append(line).append("\n");
line = br.readLine();
}
String fileAsString = sb.toString();
br.close();
return fileAsString;
}
You need to change the condition of your loop
String[] nextLine;
while((nextLine = brTest.readLine()) != null) {
...
}
ReadLine reads each line from beginning up to the occurrence of \r andor \n
You can also use tokenizer to split the string
String[] test = "this is a test".split("\\s");
In addition it seems the file is of type CSV if it is please mention that in the question.
I have following code:
private String ReadCPUinfo()
{
ProcessBuilder cmd;
String result="";
try{
String[] args = {"/system/bin/cat", "/proc/cpuinfo"};
cmd = new ProcessBuilder(args);
Process process = cmd.start();
InputStream in = process.getInputStream();
byte[] re = new byte[1024];
while(in.read(re) != -1){
System.out.println(new String(re));
result = result + new String(re);
}
in.close();
} catch(IOException ex){
ex.printStackTrace();
}
return result;
}
and String from /proc/cpuinfo as result. I need to extract processor info (Processor: WordIWantToExtract) as String to put it in the TextView.
I did it in Python script (print cpuinfo to the txt file, then lookup line number with word "Processor", return its line number and then printing this line with editing). How can I port this to the Java?
/proc/cpuinfo is just a text file. Just use a BufferedReader and read the contents instead of using ProcessBuilder. Check for the the prefix "Processor" to extract the exact line.
BufferedReader reader =
Files.newBufferedReader(Paths.get("/proc/cpuinfo"), StandardCharsets.UTF_8);
while ((line = reader.readLine()) != null) {
Matcher m = Pattern.compile("Processor: (.*)").matcher(line);
if (m.find()) {
System.out.println("Processor is " + m.group(1));
...
}
}
I would use a JSONObject. Yo ucan create the object with a "key" processor and the word you want. For example,
Map<String, String> processors = new HashMap<String, String>();
loggingMap.put("Processor", "Word");
JSONObject jsonObject = new JSONObject();
jsonObject.element(processors);
The line will look like this, {"Processor": "word", "Other Key": "Other Word"}
Then you can write this to a file,
jsonObject.write(Writer writer);
Then you can read the line from the file and use,
jsonObject.getString("Processor");
I used a HashMap incase you have keys and values.
I'm not sure to understand well your question but I think you can add this after the while loop:
Matcher matcher = Pattern.compile("Processor: (.*)").matcher(result);
if (matcher.find()) {
String wordYouWantToExtract = matcher.group(1);
}