My text file has a pattern and it's just like the following:
1;Mary Yeah;John Freeman;(12)3456-7890;iammary#gmail.com
2;Ash Wilson;One Two Three;(99)1111-2222;lorddragon#hotmail.com
3;Xin Zhao;Street Address 55;(11)0101-0202;lolyourface#gmail.com
4;My Name;My Address;My Phone;myemail#mail.com
I want to be able to type the line number, the type of data I want to replace(e-mail, phone, name), and the string I want to replace them with. The program overwrites the text.
How could I code this in Java?
The issue of how to find a given row based on the line number depends on many things, most importantly it depends on code you haven't shown us. But as for what you can do once you have found a given line, you may try the following:
String line = "2;Ash Wilson;One Two Three;(99)1111-2222;lorddragon#hotmail.com";
String[] parts = line.split(";");
parts[4] = "some.address#mail.com"; // to change the email
// now join back to a single line
line = String.join(";", Arrays.asList(parts));
Demo
Related
I tried to make security to display email data by replacing some words with symbol (*) but not as expected there might be an error in making the example script as below.
String email = "thismyemail#myhost.com";
String get_text = email.get_text(3, 6);
String hasil = email.replace(get_text,"*");
email_string = (EditText) findViewById(R.id.emailT);
email_string.setText(hasil);
But the result is like this
thi*email#myhost.com
Which I expect
thi***email#myhost.com
String hasil = email.replace(get_text,"***");
But please note that if that text appears anywhere else in the string it will be replaced as well.
Also, if the email is like jf#mymailserver.com you won't be replacing a part of their user id with *.
So you can probably find a better way to select the characters, taking into account email length and also not "replacing" text but rather putting those chars at the specific position you want to.
See this related question for some ideas on how to improve this:
masking of email address in java
Your code seems right. If ur expected output is like the one mentioned above, you can just add 2 more "*" to the code.
String hasil = email.replace(get_text,"***");
I hope it helps
I am trying to extract data out of a website access log as part of a java program. Every entry in the log has a url. I have successfully extracted the url out of each record.
Within the url, there is a parameter that I want to capture so that I can use it to query a database. Unfortunately, it doesn't seem that the web developers used any one standard to write the parameter's name.
The parameter is usually called "course_id", but I have also seen "courseId", "course%3DId", "course%253Did", etc. The format for the parameter name and value is usually course_id=_22222_1, where the number I want is between the "_" and "_1". (The value is always the same, even if the parameter name varies.)
So, my idea was to use the regex /^.*course_id[^_]*_(\d*)_1.*$/i to find and extract the number.
In java, my code is
java.util.regex.Pattern courseIDPattern = java.util.regex.Pattern.compile(".*course[^i]*id[^_]*_(\\d*)_1.*", java.util.regex.Pattern.CASE_INSENSITIVE);
java.util.regex.Matcher courseIDMatcher = courseIDPattern.matcher(_url);
_courseID = "";
if(courseIDMatcher.matches())
{
_courseID = retrieveCourseID(courseIDMatcher.group(1));
return;
}
This works for a lot of the records. However, some records do not record the course_id, even though the parameter is in the url. One such example is the record:
/webapps/contentDetail?course_id=_223629_1&content_id=_3641164_1&rich_content_level=RICH&language=en_US&v=1&ver=4.1.2
However, I used notepad++ to do a regex replace on this (in fact, every) url using the regex above, and the url was successfully replaced by the course ID, implying that the regex is not incorrect.
Am I doing something wrong in the java code, or is the java matcher broken?
I am following the guide of Cascading on its website. I have the following TSV format input:
doc_id text
doc01 A rain shadow is a dry area on the lee back side of a mountainous area.
doc02 This sinking, dry air produces a rain shadow, or area in the lee of a mountain with less rain and cloudcover.
doc03 A rain shadow is an area of dry land that lies on the leeward (or downwind) side of a mountain.
doc04 This is known as the rain shadow effect and is the primary cause of leeward deserts of mountain ranges, such as California's Death Valley.
doc05 Two Women. Secrets. A Broken Land. [DVD Australia]
I use the following code to process it:
Tap docTap = new Hfs(new TextDelimited(true, "\t"), inPath);
...
Fields token = new Fields("token");
Fields text = new Fields("text");
RegexSplitGenerator splitter = new RegexSplitGenerator(token, "[ \\[\\]\\(\\),.]");
// only returns "token"
Pipe docPipe = new Each("token", text, splitter, Fields.RESULTS);
It looks like just split the second part of each line (ignore doc_id part). How does Cascading ignore the first doc_id part and just process the second part? is that because of TextDelimited ??
If you see the pipe statement
Pipe docPipe = new Each("token", text, splitter, Fields.RESULTS);
The second argument is the only field you are sending to splitter function. Here you are sending 'text' field. SO only the text is sent to splitter and returns the tokens.
Below explains the Each method clearly.
Each
#ConstructorProperties(value={"name","argumentSelector","function","outputSelector"})
public Each(String name,
Fields argumentSelector,
Function function,
Fields outputSelector)
Only pass argumentFields to the given function, only return fields selected by the outputSelector.
Parameters:
name - name for this branch of Pipes
argumentSelector - field selector that selects Function arguments from the input Tuple
function - Function to be applied to each input Tuple
outputSelector - field selector that selects the output Tuple from the input and Function results Tuples
The answer is in these 2 lines
1. The way Tap was created, program was told that first line contains header ("true").
Tap docTap = new Hfs( new TextDelimited( true, "\t" ), docPath );
2. And second, in this line the column name was provided as "text". If you look closely in your input file, "text" is the column name for the data you are trying to base your word count on.
Fields text = new Fields( "text" );
This link: http://www.otc.edu/GEN/schedule/all_classes_fall.txt contains classes for my college, and I am trying to take all of this data and store it in a ClassInformationFall object I have created. Basically, the classes begin at the class title in the format like this : "ABR-100-101" and have class instructor, days it occurs, start/end time, etc.
I have written some regex to pick out the class title, and some of the easier things like start and ending time, but I have been struggling on trying to get the rest of it out. I was thinking about setting up some code where anytime another class title is encountered, it adds the following text to a new ClassInformationFall object, which I am storing in a list of that type. Even if I had that, though, I still haven't been able to successfully extract all of the data for all of the things that make up the class.
What would be the regex to pick this information out, or is regex even the way to go?
Thanks for any help, this has stumped me for awhile.
PS - I am developing the application using this in Java.
If the fields are always in the same order, you can just split each line by tabs and deal with the resulting array.
String line = bufferedReader.readLine();
while (line != null) {
String[] data = line.split("\\t+");
String name = data[0];
String credits = data[2];
String description = data[3];
String professor = data[11];
ClassInfo ci = new ClassInfo(name, credits, description, professor);
classInfoList.add(ci);
line = bufferedReader.readLine();
}
I am not posting any code I am struck with. I am trying this in Java:
Issue:
I have words like:
,xxxx-1223
yyyyy,xxdd-345
$,xxxxr-7
sdsdsdd-18
so what ever format I have I should be able to read the last one:
xxxx-1223
xxdd-345
xxxxr-7
sdsdsdd-18
what so may be the words, all I need to to get the words as shown.
Use String#lastIndexOf(int) to find where the last comma occurs, and use String#substring(int) to get the rest of the string that follows.
String input = /* whatever */;
int lastComma = input.lastIndexOf(',');
String output = input.substring(lastComma + 1);
String[] str=yourWord.split(",");
String output=str[str.length-1];
You can use this Regex: -
(\\w+-\\d+)$
Or this specific problem can simply be solved using String.split() or String.substring(int) methods