Regex Replacing Entire Text Rather Than Match
Feb 14, 2014
With the code below, I am trying to replace all regex matches for visa cards within a given text file.
My first test was with a text "new3.txt" exclusively containing the visa test card 4111111111111111. My objective was to replace the card with "xxxx-xxxx-xxxx-xxxx". This was successful.
However, when modifying the text file to include other characters and text before and after (ex: " qwerty 4111111111111111 adsf zxcv"), it gives mixed results. Although it successfully validates the match, it replaces the whole text in the file, rather than replacing solely the match.
When trying this search and replace with words (rather than a regex match), it does not have this behavior. What am I missing?
import java.io.*;
import java.util.regex.*;
public class BTest
{
//VISA Test
private final static String PATTERN = "(?s).*4[0-9]{12}(?:[0-9]{3})?.*";
public static void main(String args[])
{
try
[Code]...
View Replies
ADVERTISEMENT
Oct 4, 2014
In a code im writing I was curious as to instead of
contains(".")
I wanted to use
matches(" ") method instead but really having issues with the regex
I have tried //. as well as ^[.] but they give me errors.
I am just trying to match a period but was curious as to what the form was. I read a good amount of materials and seemed simple but getting errors.
View Replies
View Related
Oct 15, 2014
I have 2 strings
String value ="/abc_12_1/abc234/abc/filename.txt";
String src="abc"
Sring tgt=xyz
Now I have to replace the string in variable value in case of exact match; I am trying to do the following :
if(value.contains(src)
{
value.replaceall(src,tgt);
}
Now the problem here is it replaces all the occurrence of abc in the string value and I get the below output as :
value=""/xyz_12_1/xyz234/xyz/filename.txt";
However my requirement is only in the case the value exactly matches with source the replacement shd happen. I am expecting the output like this :
String value ="/abc_12_1/abc234/xyz/filename.txt";
Also the above code is in a function which will be called multiple times and the values will keep on changing. However the target and source will remain the same always.
View Replies
View Related
Apr 3, 2014
I have a table which contains list of regular expression and its corresponding value.I have to fetch those value and put it HASHMAP where regex as key.I have to compare the each key with the given string(input) and If matches I have to get the corresponding Value for the regex.
View Replies
View Related
Apr 19, 2014
I am trying to match validate user input via regex comparison. But i fail to do so.
The string that will be inserted will have the following pattern: "digit/s " and it can be repeated a random number of times.
The program will extend to characters in the future but for now i am struggling with digits.
i am using a StringBuilder to build the regex string. Or maybe i could use something else but so far i didn't make it.
So far i have this regex
Java Code: "(d+s+)+" mh_sh_highlight_all('java');
The problem is that the newline character will interfere with the last number.
So if i enter
Java Code: "3 4 555" mh_sh_highlight_all('java');
- it will not match. And the regex will only see 3 4.
But if i enter
Java Code: "3 4 555 " mh_sh_highlight_all('java');
- with an extra space at the end it works. Obviously that is not the desired case.
I did find a workaround, but i fear it is somewhat broken
I changed to
Java Code: "(d+s*)+" mh_sh_highlight_all('java');
But in this case the pattern should see 3 4 5 5 5 as the regular expression. Am i correct?
I also tried some other variants but none of them worked:
Java Code: "d+(s+|*)" mh_sh_highlight_all('java');
Or i could do something like
Java Code: "(d+s){" + i + "}" mh_sh_highlight_all('java');
- where i is the number of values i enter.
Am i on the right track here? What am i forgetting?
View Replies
View Related
Apr 5, 2014
I have an assignment on sorting, i kno i can get the sorting down but im having an issue with inputing the 512 ints in a file into an array. the instructor provided us with a file with 4 equal sets of ints. i tried to make my array of size [scan.nextInt()] and it cuts off the last 21 ints. and skips the first int. how can i get all of the integers in the text file into my array? this is what i have so far. if i hard code the array to size 50000 and then try to print the array it compiles but errors out when running it.
System.out.println("Please Enter text file in this format, XXXXX.txt :");
String file =fileName.nextLine();
Scanner scan = new Scanner(new File(file));
int [] data = new int[scan.nextInt()]; <-------here it skips first int
int count= data.length;
for (int i=0; i<data.length-1;i++) {
data[i]=scan.nextInt();
}
System.out.print(Arrays.toString(data));
rst 4 ints in output are: 501, 257, 390, 478...., supposed to be 492,501,390....and last ints are: ....88, 83, 79, 0 and supposed to be :88 83 79 77 76 72 71 71 66 57 56 48 48 41 33 30 23 23 18 17 15 13 9....it replace last ints with 0. why ? and how do i fix this. attached it the text file
View Replies
View Related
Jun 29, 2014
public class Regextest {
public static void main(String a[]){
String stream = "ab34ef";
Pattern pattern = Pattern.compile("d*")
[code]....
My question is: The methods start()will give the indexes into the text where the found match starts .d*, this means if a number found.index must be returned. Why doesn't it print 3? 12345
Another question: Why it prints 6?
0>a
.
.
.
5>f
What is this 6 in out put?
View Replies
View Related
Feb 18, 2014
I have a large text file of 1 GB size. I need to print the line when a matching word is found in a particular line. Below is the code I am using. But if there are many lines that has the matching word, it's taking lot of time. Any solution to print the lines much faster.
Scanner scanner = new Scanner(file);
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
if(line.contains("xyz")) {
System.out.println(line);
}
}
View Replies
View Related
Mar 7, 2014
Is that the only way to code the JOptionPane? Seems rather similar to Sys.out
--- Update ---
I had more than this, but post was deleted somehow and this was the gist of it:
for(i = 0 ; i < charArray.length; i++){
if(Character.isDigit(charArray(i))){
address.replace("6", "*");
address.replace("7", "*");
Is there a better way to scan a character array for digits and replace any number in the array with another character ("*") in this case? The array is the address, " 1234 Runner Road " .
View Replies
View Related
Mar 29, 2014
I'm new to Java and have not been able to figure this out. I'm reading a text file that contains this:
110001 commercial 500000.00 101
110223 residential 100000.00 101
110020 commercial 1000000.00 107
110333 land 30000.00 105
110442 farm 200000.00 106
110421 land 40000.00 107
112352 residential 250000.00 110
The output goes to a text file and should look like this:
COMMERICAL
FARM
LAND
RESIDENTIAL
101 600000.00
105 30000.00
106 200000.00
107 1040000.00
110 250000.00
I'm creating the output file, but it only contains the categories and not the details below them. Cant seem to figure out why.
Java Code:
package propertylistings;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
[Code] ....
View Replies
View Related
May 24, 2014
how to replace the values in my array with the results of my function factorial.
public static void main(String[] args) {
//this is my main function:
int[] array = {5,4,3,2,1};
int i = 0;
System.out.print("results: ");
for (i = 0; i < array.length; i++){
System.out.print(factorial(array[i]));
[code]....
So, what I'm trying to do is change the contents of the array "array" into their factorial value. So, they should be replaced with {120,24,6,2,1}. then add those using linear sum but that's a different story.
View Replies
View Related
Mar 18, 2014
I need to create a regexp, that will do the following:
a,a,a,a,c - matches
c,a,a,a,a - matches
a,a,a,a,a - doesn't match
I will be using it in Java. In the place of 'a', can be 'b' - they are equal. Also, in the place there can be any other character. This is what i have came up with:
^(?=^(a|b)).*((a|b),){4}(?!(a|b))|(?!(a|b)).*(((a| b),*){4})$
It fails because it matches the 5 a's. I'm quite new to regexp, so I'm not aware of all the possibilities. It matches the 5 a's, because the first if fails, but the second does not. Maybe there is a simpler way to accomplish this? (Also why are the .* necessary in the middle?)
View Replies
View Related
Feb 4, 2015
I built a GUI and after testing it on various platforms I think I would like to make it bigger by a factor of 2.2 or so.
Is there an easy way to just enter the factor 2.2 and have the GUI change by that factor?
I have over 50 components in the GUI. Ideally each one would just enlarge by the factor including the fonts.
This was my first (and only so far) java program. I didn't know anything about layout managers. I used netbeans and it generated all the code.
View Replies
View Related
Jul 24, 2014
so my task is to write a code which would ask user to input the year as integer and first three letters of the month with first being an upper case letter. Than the program would determine the number of days for given year and month.
Currently I have a brain crash on how to check, if the user has provided the month with first character being upper Case. If not, than the program would automatically correct the character. Problem starts at line 17.
import java.util.Scanner;
public class DaysOfMonth4_17 {
public static void main (String[] args) {
//Initiate scanner
Scanner input = new Scanner (System.in);
//Ask for year input and use is as INT
System.out.println("Enter the year");
[code]...
View Replies
View Related
Apr 16, 2014
I'm currently trying to build a DAO based application where you use a text file as a data source. It have worked out well until I tried to delete lines from the file.
public void delete(DTOBil dtobil) {
try{
reader = Files.newBufferedReader(Paths.get("databilar.txt"),charset);
writer = Files.newBufferedWriter(Paths.get("temp.txt"), charset, StandardOpenOption.CREATE_NEW);
String line = null;
while((line = reader.readLine()) != null){
[Code] ....
I've managed to fill out the temp file with everything except the line I wanted to remove, but when I try to replace the original file with the temp file it won't work. It casts the error: "temp.txt -> databilar.txt".
I've also tried to use the renameTo method without any success...
View Replies
View Related
Apr 23, 2014
I'm trying to use regex to find a word containing the substring "cat." I'm not sure why this isn't working?
String match = "^ cat$";
View Replies
View Related
Jun 11, 2014
I would like to test whether the first word in a line is is a valid var name (e.g sequence of letters, digits and "_", it may not start with a digit, if "_" is the first char it must be followed by a letter). I tried this simple code:
String namePattern = "_[a-zA-Z0-9]+";
String text = "__c";
Pattern pattern = Pattern.compile(namePattern);
Matcher matcher = pattern.matcher(text);
"__c" is an illegal var name.
But it returns the string "_c", where it is supposed to return an empty matcher.
where am I wrong here?
View Replies
View Related
Apr 4, 2014
I am trying to add an email validator to my xhtml page, but I get this error:
<f:validateRegEx> Tag Library supports namespace: [URL] ...., but no tag was defined for name: validateRegEx
This is my code:
<h:inputText tabindex="7" styleClass="input" id="email" value="#{user.email}"
required="true" validatorMessage="Invalid email!">
<a4j:support id="emailRenderer" event="onblur"
reRender="emailPanel, errorPanel"
ajaxSingle="true"/>
<f:validateRegEx pattern="[w.-]*[a-zA-Z0-9_]@[w.-]*[a-zA-Z0-9].[a-zA-Z][a-zA-Z.]*(com|net|org|edu)" />
</h:inputText>
<p:message for="email" />
[Code] ....
View Replies
View Related
Jun 13, 2014
I have one column "category" which contain data like
`"Failed extract of third-party root list from auto update cab at: [URL] with error: The data is invalid."`
I need to select url part in between `" < > "` sign of category column.
how to do this usign regex?
View Replies
View Related
Apr 14, 2014
I am in need of regex for alphanumeric (uppercase only) values which will verify string of length 5below
ABCCD - False
AB12C - True
ABC12 - True
12ABC - True
12345 - False
If it contain any lowercase then it should result in false as well.
My regex ^[A-Z0-9]{5}$ is not working for above type of values.
View Replies
View Related
Sep 12, 2014
I am trying to write a regular expression for a text which has both String and number... like abc1234, xyz987, gh1052 etc. And the string usually contains 2 or 3 characters.
What I need is two Strings one containing the text (abc, xyz, gh etc) and other containing number (1234, 987, 1052, etc.). Have written the code below. but doesn't seem to work.
String query = "abc1052"; //Or query = "zyx900";
String regexp = "(.{3})(d*)|(.{2}(d*))";
Pattern pattern = Pattern.compile(regexp);
Matcher match = pattern.matcher(query);
if(match.find()){
[Code] ....
Also tried with regexp = "(.s*)(.d*)" in no avail.
Finally achieved with regexp = "(D*)(d*)";
View Replies
View Related
Oct 23, 2014
I'd like to enable/disable a jpanel and it's entire contents in one fell swoop. I could of course call each component's .setEnabled() method, but I figured there must be a better way!
View Replies
View Related
Apr 1, 2014
I have in my app a JTable. I would like to know how can i do to, when i click in a cell, my jtable paint the entire line of this cell. Here is what i did.
Method call:
private void jTable1MouseClicked(java.awt.event.MouseEvent evt) {
int row = jTable1.rowAtPoint( evt.getPoint() );// Don't know how to send this info through setDefaultRenderer.
jTable1.setDefaultRenderer( Object.class, new RowRender() );
[Code] ....
View Replies
View Related
Jun 15, 2014
I have one quick question. If you are extending the acm graphics program, how can you set the location of the entire window? All the set location methods within the graphics program only seem to set the location of graphical objects. I know with JFrames I can use the setLocationRelativeTo(null) to position the JFrame in the center. How to set the location of a graphics window.
View Replies
View Related
Aug 13, 2014
make the keylistener "listen" across the entire application? Currently whenever I've used it I have always attached it to something else, like a JTextField or the like. However that doesn't work all that well if I for example want to close down the current JDialog by pressing escape, because no object that has the keylistener attached to it is currently focused.
When I searched around for the answer if found similar questions on stackoverflow, but those seemed to focus on listening globally, even when the application itself is not focused, which isn't quite what I want.
Here is a simple piece of code that I'm currently experimenting with:
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.IOException;
import java.security.spec.InvalidKeySpecException;
[Code].....
It's supposed to simply create a frame and give me a message whenever I press the A or D buttons, as well as notify me whenever they are released. As of currently it obviously doesn't work because the keylistener hasn't been attached to anything, but that's also where I'm unsure of how to proceed.
View Replies
View Related
Feb 18, 2014
I have a file which contains certain positions ([a][b]) that require to be placed in a certain multi-dimensional array. For example I might have an array String[][] that is a size of 6x6 but only have values in positions [2][1] and [3][2]. Because it is important for me to maintain the given array size and also do certain actions with the given positions I cannot modify the size. In addition I need to count the surrounding neighbors each element has (including elements that are null). However because some of my further code cant process with null elements I need to remove all null elements with " " (blank).
I am not sure how this is done or if it's even possible. If it is not possible how can I do something as close as possible to my needs?
View Replies
View Related