Strange behavior with Regex in Java - java

I want to filter a text, leaving only letters (a-z and A-Z). It seemed to be easy, following something like this How to filter a Java String to get only alphabet characters?
String cleanedText = text.toString().toLowerCase().replaceAll("[^a-zA-Z]", "");
System.out.println(cleanedText);
The problem that the output of this is empty, unless I change the regex, adding another character, e.g. : --> [^:a-zA-Z]
I allready tried to check if it works with normal regex (not using the method ReplaceAll given by String object in Java), but I had exactly the same problem.
Any idea what could be the source of this strange behavior?
I had a txt file which I read using a BufferedReader. I add each line to one long string and apply the code I posted before to this. The whole code is as follows:
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.lang.StringBuffer;
import java.util.regex.*;
public class Loader {
public static void main(String[] args) {
BufferedReader file = null;
StringBuffer text = new StringBuffer();
String str;
try {
file = new BufferedReader(new FileReader("text.txt"));
} catch (FileNotFoundException ex) {
}
try
{
while ((str = file.readLine()) != null) {
text.append(str);
}
String cleanedText = text.toString().toLowerCase().replaceAll("[^:a-z]", "");
System.out.println(cleanedText);
} catch (IOException ex) {
}
}
}
The text file is a normal article where I want to delete everything (including whitespaces) that is not a letter. An extract is as follows "[16]The Free Software Foundation (FSF), started in 1985, intended the word "free" to mean freedom to distribute"

as I wrote in a comment, specify more precisely what's wrong...
What I tried
public class Regexp45348303 {
public static void main(String[] args) {
String[] tests = { "abc01", "01DEF34", "abc 01 def.", "a0101\n0202\n0303x" };
for (String text : tests) {
String cleanedText = text.toLowerCase().replaceAll("[^a-z]", ""); // A-Z removed too
System.out.println(text + " -> " + cleanedText);
}
}
}
and the output is:
abc01 -> abc
01DEF34 -> def
abc 01 def. -> abcdef
a0101
0202
0303x -> ax
which is correct based on my understanding...

In the end the problem was not with the regex nor with the program itself. It was just that eclipse does not show the output in console if it exceeds a certain length (but you can still work on it). To solve this simply check the fixed width console in Window -> Preferences -> Run/Debug -> Console
as described in http://code2care.org/2015/how-to-word-wrap-eclipse-console-logs-width/
Image of where to check fixed width console checkbox

Related

javax.swing.UIManager.getIcon(Object key) returns null when String-keys are streamed from an ArrayList

What I want to achieve:
I want to visualize some javax.swing.Icons from the javax.swing.UIManager. On the internet I've found a list of UIManager-keys, which will not only return Icons but also Strings, Colors and so on. So in this step I want to filter the list of keys so only the Icon-keys remain.
My Approach:
I copied a list of UIManager keys into a textfile and included it as recource in my Java-Project. I successfully read the file so I split the file-content by lines and added them to an ArrayList of Strings. Now i wanted to stream the content of this ArrayList and filter the keys by wether the UIManager.getIcon(Object key)-Method returns null or not...
My Problem
so far: the UIManager always returns null. I printed all the keys and the UIManager result to the console (see "Output / Test - stream keys" in my code). If i manually copy a key from the console (one that I know should work) and paste it into the exact same piece of code, it actually works (see "Output / Test - single Key" in my code).
Interesting Behavior shows when I append a String to the key that I want to print to the console (See the variable "suffix" under "Output / Test - stream Keys" in my code). If the variable suffix does not start with "\n", the following print-Method in the stream will only print the suffix and no longer show the other content. For example if I type String suffix = "test"; only "test" will be printed from the .forEach(key->System.out.println(... + key + suffix); However, this behavior does not show up in the "Output / Test - single Key"-Example.
I have no idea, what is going on or if the (in my opinion) strange behavior as anything to do with the problem. I appreciate any kind of help!
Piece from "UIManagerKeys.txt":
Here are some keys for testing and reproducibility purposes...
FileView.computerIcon
FileView.directoryIcon
FileView.fileIcon
FileView.floppyDriveIcon
FileView.hardDriveIcon
FormattedTextField.background
FormattedTextField.border
windowText
My Code:
package main;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Arrays;
import javax.swing.UIManager;
public class Main {
public Main() {
}
public static void main(String args[]) {
ArrayList<String> uiKeys = new ArrayList<>();
String fileName = "recources/UIManagerKeys.txt";
ClassLoader classLoader = new Main().getClass().getClassLoader();
File file = new File(classLoader.getResource(fileName).getFile());
// Check: is File found?
System.out.println("File Found : " + file.exists());
try {
// Read File Content
String content = new String(Files.readAllBytes(file.toPath()));
// Split by line and collect
String[] keys = content.split("\n");
uiKeys.addAll(Arrays.asList(keys));
} catch (IOException e) {
System.out.println("Error: " + e);
}
// Output / Test - stream Keys
System.out.println("Total Number of Keys: " + uiKeys.size());
String suffix = ""; // Interesting behavior when String is not empty
uiKeys.stream()
.map(key -> key.replaceAll(" ", "").replaceAll("\n", "")) // Just to be sure
.forEach(key -> System.out.println("IconFound: " + (UIManager.getIcon(key) != null) + "\tKey: " + key + suffix));
// Output / Test - single Key
System.out.println("\n");
String key = "FileView.directoryIcon"; // Copied from console
System.out.println("IconFound: " + (UIManager.getIcon(key) != null) + "\tKey: " + key + suffix);
}
}
I want to visualize some javax.swing.Icons from the javax.swing.UIManager.
I copied a list of UIManager keys into a textfile
There is no need to create the text file. You just get all the properties from the UIManager and check if the Object is an Icon:
public static void main(String[] args) throws Exception
{
UIDefaults defaults = UIManager.getLookAndFeelDefaults();
for ( Enumeration enumm = defaults.keys(); enumm.hasMoreElements(); )
{
Object key = enumm.nextElement();
Object value = defaults.get( key );
if (value instanceof Icon)
System.out.println( key );
}
}
On the internet I've found a list of UIManager-keys,
Check out: UIManager Defaults for a little graphical tool that displays all the properties.
Your approach of reading the files seems a little complicated. Here is a simple approach to reading the lines of a file into a ListArray:
import java.io.*;
import java.util.*;
public class ReadFile
{
public static void main(String [] args) throws Exception
{
ArrayList<String> lines = new ArrayList<String>();
BufferedReader in = new BufferedReader( new FileReader( "ReadFile.java" ) );
String line;
while((line = in.readLine()) != null)
{
lines.add(line);
}
in.close();
System.out.println(lines.size() + " lines read");
}
}

Loading file, checking for duplicates, skipping duplicates and printing them out at the end in java

I am new to Java and i need to find a way to check if an incoming file has duplicate lines, skip those lines and not load them, and then at the end print out a message saying what the duplicates were.
Please help.
In this case, you might need to read one line and compare with another file to line by line. Normally, you can use .equal, but you need to make sure you are reading one lie and it has been excluded change line code such as ¥n or ¥r. You had better to check the value using debugger. Better to share your code if you need further help.
Here's something I quickly whipped up which might help you out:
import java.util.ArrayList;
import java.io.*;
public class DuplicateChecker {
public static void main(String[] args) {
ArrayList<String> lines = new ArrayList<>();
try {
BufferedReader in = new BufferedReader(new FileReader("testfile"));
String line_read = "";
line_read = in.readLine();
while(line_read != null) {
if ( lines.contains(line_read) ) {
System.out.println("Duplicate found: " + line_read);
}
lines.add(line_read);
line_read = in.readLine();
}
} catch (FileNotFoundException fnf_ex) {
} catch (IOException io_ex) {
}
}
}

Remove a character followed by whitespace each newline of a string

I am writing a program to edit a rtf file. The rtf file will always come in the same format with
Q XXXXXXXXXXXX
A YYYYYYYYYYYY
Q XXXXXXXXXXXX
A YYYYYYYYYYYY
I want to remove the Q / A + whitespace and leave just the X's and Y's on each line. My first idea is to split the string into a new string for each line and edit it from there using str.split like so:
private void countLines(String str){
String[] lines = str.split("\r\n|\r|\n");
linesInDoc = lines;
}
From here my idea is to take each even array value and get rid of Q + whitespace and take each odd array value and get rid of A + whitespace. Is there a better way to do this? Note: The first line somteimes contains a ~6 digit alphanumeric. I tihnk an if statement for a 2 non whitespace chars would solve this.
Here is the rest of the code:
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;
import javax.swing.JEditorPane;
import javax.swing.text.BadLocationException;
import javax.swing.text.EditorKit;
public class StringEditing {
String[] linesInDoc;
private String readRTF(File file){
String documentText = "";
try{
JEditorPane p = new JEditorPane();
p.setContentType("text/rtf");
EditorKit rtfKit = p.getEditorKitForContentType("text/rtf");
rtfKit.read(new FileReader(file), p.getDocument(), 0);
rtfKit = null;
EditorKit txtKit = p.getEditorKitForContentType("text/plain");
Writer writer = new StringWriter();
txtKit.write(writer, p.getDocument(), 0, p.getDocument().getLength());
documentText = writer.toString();
}
catch( FileNotFoundException e )
{
System.out.println( "File not found" );
}
catch( IOException e )
{
System.out.println( "I/O error" );
}
catch( BadLocationException e )
{
}
return documentText;
}
public void editDocument(File file){
String plaintext = readRTF(file);
System.out.println(plaintext);
fixString(plaintext);
System.out.println(plaintext);
}
Unless I'm missing something, you could use String.substring(int) like
String lines = "Q XXXXXXXXXXXX\n" //
+ "A YYYYYYYYYYYY\n" //
+ "Q XXXXXXXXXXXX\n" //
+ "A YYYYYYYYYYYY\n";
for (String line : lines.split("\n")) {
System.out.println(line.substring(6));
}
Output is
XXXXXXXXXXXX
YYYYYYYYYYYY
XXXXXXXXXXXX
YYYYYYYYYYYY
If your format should be more general, you might prefer
System.out.println(line.substring(1).trim());
A BufferedReader will handle the newline \n for you.
You can use a matcher to validate that the line is in the desired format.
If the line is fixed length, simply use the substring
final String bodyPattern = "\\w{1,1}[ \\w]{5,5}\\d{12,12}";
try (BufferedReader br = new BufferedReader(new FileReader(fileName))) {
String line;
while ((line = br.readLine()) != null) {
if (line.matches(bodyPattern)) {
//
myString = line.substring(6);
}
}
}
//catch Block
You can adjust the regex pattern to your specific requirements
easily doable by a regex (assuming 'fileText' is your whole file's content)
removedPrefix = fileText.replaceAll("(A|Q) *(.+)\\r", "$2\\r");
The regex means a Q or A for start, then some (any amount of) spaces, then anything (marked as group 2), and a closing line. This doesn't do anything to the first line with the digits. The result is the file content without the Q/A and the spaces. There are easier ways if you know the exact number of spaces before your needed text, but this works for all, and greatly flexible.
If you process line by line it's
removedPrefix = currentLine.replaceAll("(A|Q) *(.+)", "$2");
As simple as that

Copy part of line from 1 text to 2nd text file

I am working on project where I need to copy some part of each line from 1st text file to other. In the first text each data is separated by splitter --# (which i have used).
I want to get the 1st two parts of actual 4 parts total of 3 splitters Ex:
Hello--#StackOverflow--#BMWCar--#Bye.
I just want to fetch 1st 2 parts .ie.
Hello--#StackOverflow
from all the lines of first text file to second text file. I have tried everything and could not get it to work. Please help me out of this. :)
I am little late, but below code will work as well :
String str = "Hello--#StackOverflow--#BMWCar--#Bye.";
String strResult = str.split("(?<!\\G\\w+)(?:--#)")[0];
System.out.println(strResult);
\G is previous match, (?<!regex) is negative lookbehind.
[Update]
In your case, can we use below code? The solution is based on the file you provided
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class Test{
public static void main (String[] args) throws IOException
{
BufferedReader in = new BufferedReader(new FileReader("abc.txt"));
String line;
while((line = in.readLine()) != null)
{
System.out.println(line.substring(0,line.lastIndexOf("--#")));
}
in.close();
}
}
For each line you read do the following:
String[] sp = line.split("--#",2);
String result = sp[0]+"--#"+sp[1];
If you're expecting names with dashes, it seems to me the best thing to use is String.split:
String test = "Hel-lo--#StackOverflow--#BMWCar--#Bye";
String[] sp = test.split("--#");
for(String name : Arrays.copyOfRange(sp, 0, 2))
{
System.out.println(name);
}
First, to get all the lines in the text file, try using a while loop. As you are reading in from one file, output to the second file. Split each line based on the conditional.
String inLine = in.nextLine();
String[] parts = inLine.split("--#");
String toWrite = parts[0] + " " + parts[1];
outLine.write(toWrite);
Should be easy enough to figure out.

How can I parse through a file for a string matching a generated string?

My bad for the title, I am usually not good at making those.
I have a programme that will generate all permutations of an inputted word and that is supposed to check to see if those are words (checks dictionary), and output the ones that are. Really I just need the last the part and I can not figure out how to parse through a file.
I took out what was there (now displaying the "String words =") because it really made thing worse (was an if statement). Right now, all it will do is output all permutations.
Edit: I should add that the try/catch was added in when I tried turning the file in a list (as opposed to the string format which it is currently in). So right now it does nothing.
One more thing: is it possible (well how, really) to get the permutations to display permutations with lesser characters than entered ? Sorry for the bad wording, like if I enter five characters, show all five character permutations, and four, and three, and two, and one.
import java.util.List;
import java.util.Scanner;
import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import static java.lang.System.out;
public class Permutations
{
public static void main(String[] args) throws Exception
{
out.println("Enter anything to get permutations: ");
Scanner scan = new Scanner(System.in);
String io = scan.nextLine();
String str = io;
StringBuffer strBuf = new StringBuffer(str);
mutate(strBuf,str.length());
}
private static void mutate(StringBuffer str, int index)
{
try
{
String words = FileUtils.readFileToString(new File("wordsEn.txt"));
if(index <= 0)
{
out.println(str);
}
else
{
mutate(str, index - 1);
int currLoc = str.length()-index;
for (int i = currLoc + 1; i < str.length(); i++)
{
change(str, currLoc, i);
mutate(str, index - 1);
change(str, i, currLoc);
}
}
}
catch(IOException e)
{
out.println("Your search found no results");
}
}
private static void change(StringBuffer str, int loc1, int loc2)
{
char t1 = str.charAt(loc1);
str.setCharAt(loc1, str.charAt(loc2));
str.setCharAt(loc2, t1);
}
}
If each word in your file is actually on a different line, maybe you can try this:
BufferedReader br = new BufferedReader(new FileReader(file));
String line = null;
while ((line = br.readLine()) != null)
{
... // check and print here
}
Or if you want to try something else, the Apache Commons IO library has something called LineIterator.
An Iterator over the lines in a Reader.
LineIterator holds a reference to an open Reader. When you have finished with the iterator you should close the reader to free internal resources. This can be done by closing the reader directly, or by calling the close() or closeQuietly(LineIterator) method on the iterator.
The recommended usage pattern is:
LineIterator it = FileUtils.lineIterator(file, "UTF-8");
try {
while (it.hasNext()) {
String line = it.nextLine();
// do something with line
}
} finally {
it.close();
}

Categories