I am writing a setter for a domain class. What is being saved is an xml that is a response from a web service. It includes the first and last name of the user but that information needs to be masked. So i am trying to accomplish that using regex.
I wrote the following setter method:
public void setOnlineRetroCreditResponse(String xml) {
xml.replaceAll (/(?<=lastName=)([^\s]+)/){lastName ->
lastName[0].replace ( lastName[1], "X".multiply (lastName[1].size()))
}
onlineRetroCreditResponse = xml
}
I am expecting a sting like this one: "FFPAccountNumber2=12345 lastName=Doe" to be substituted and saved to the databse like this "FFPAccountNumber2=12345 lastName=XXX" but that is not working as expected. I tested my regex using different online like this one https://www.freeformatter.com/java-regex-tester.html and that does not seem to be the issue.
Any ideas would be appreciated.
There are two things: 1) you do not assign the replaced value back to the xml variable and 2) you are replacing the match explicitly while you can just do the necessary modifications to the value you have captured to return it.
Actually, you do not even need to capture the non-whitespace character chunk, you may access the whole match in the replaceAll callback. Also, you can use \S instead of [^\s].
Use
public void setOnlineRetroCreditResponse(String xml) {
onlineRetroCreditResponse = xml.replaceAll(/(?<=lastName=)\S+/){lastName ->
"X".multiply(lastName[0].size())
}
}
Related
I'm working on a school assignment for my compiler and interpreters course and our current task is to create a scanner and a set of tokens using JavaCC. I have a pretty solid understanding of how JavaCC works but my problem is finding resources online to help me out when I get stuck. I am working on creating a custom Token class, let's call it NewToken.Java. I know that the base Token class has an image variable and a kind variable but I want to implement my own variable "value". Furthermore I want to figure out how I can assign this value. I want the value variable to hold the literal value of what I scan, for example, my NewToken is being matched to the following
< IDENTIFIER:(< LETTER >)+ ( < LETTER > | < DIGIT >)* >
< #LETTER:["a" - "z"] >
< #DIGIT: ["0" - "9"] >
so something along the lines of Name123Name would get caught and when it does I want to store the string "Name123Name" into the 'value' variable of my NewToken object. I hope this makes sense, I am still new to JavaCC and may be calling things by there wrong name here.
public NewToken(){}
public NewToken(int kind){
this(kind,null);
}
public NewToken(int kind, String image){
this.kind=kind;
this.image=image;
this.value=image;
}
public String toString(){
return image;
}
public static Token newToken(int ofKind, String image){
switch(ofKind){
default : return new Token(ofKind, image);
}
}
public static Token newToken(int ofKind){
return newToken(ofKind, null);
}
}
Above is part of my code for the NewToken class, I have it extending Token and implementing java.io.serializable. I created by using the code generated for Token.java. I also have my variable declarations and my getValue() function which are not listed here to save space. I'm not looking for anyone to do my work for me I just need some guidance on how I would get this working, thank you in advance.
First off, I think the newToken routine should return objects of type NewToken rather than Token.
public static Token newToken(int ofKind, String image){
return new NewToken(ofKind, image);
}
public static Token newToken(int ofKind){
return new NewToken(ofKind, null);
}
(I don’t think you need that second method. But, I’m not completely sure, so I’ll leave it.)
It’s a bit unclear to me how you want value to differ from image, but I’m going to assume that you can compute the desired value for value from the image and the kind. And I’ll further assume that you have implemented this function as a static method.
private static String computeValue(int kind, String image) {...}
Delete the first two constructors and the remaining one should be:
private NewToken(int kind, String image){
this.kind = kind;
this.image = image;
this.value = computeValue( kind, image );
}
The answer that Professor Norvell is giving you is based on using a very old, obsolete version of JavaCC. The way he's suggesting you go about things is probably about the best way of doing it, if you were going to use the legacy JavaCC.
However, the most advanced version of JavaCC is JavaCC 21 and it handles this sort of use case straight out of the box in a very clean, elegant manner. See here for information on this specifically.
As you can see, you can put annotations in your grammar file that cause the various Token subclasses to be generated and used.
Also, JavaCC 21 has code injection that allows you to inject code directly into the any generated files, including the Token subclasses. That feature is not at all present in legacy JavaCC either. But using that, you could just inject your computeValue method right into the appropriate Token subclass.
INJECT NewToken :
{
private static String computeValue(int kind, String image) {...}
}
You put that in your grammar and the computeValue method just gets inserted into the generated NewToken.java file.
By the way, there is an article about JavaCC 21 that appeared recently on dzone.com.
My goal is to correct common grammar errors in messages. Here's what I currently have written:
#EventHandler
public void onChat(AsyncPlayerChatEvent event){
String message = event.getMessage().replaceAll("(?i)dont", "don't")
.replaceAll("(?i)youre", "you're");
event.setMessage(message);
}
This works to replace dont with don't, and youre with you're. The issue is that DONT is replaced with don't, rather than DON'T.
How would I execute this replacement while preserving case?
Use capturing groups:
> "DoNt".replaceAll("(?i)\\b(don)(t)\\b", "$1'$2")
"DoN't" (String)
> "YoUrE".replaceAll("(?i)\\b(you)(re)\\b", "$1'$2")
"YoU'rE" (String)
You should also use \b for a word boundary, so you don't inadvertently change words like "orthodontist" into "orthodon'tist".
I have a Cucumber step which I want to perform depending on the statement passed in Cucumber step
verify word does exists in log
clear the logs
or I may pass
verify word does not exists in log
not clear the logs
and Gherkin for this would be
("^verify word does(not|) exists in log$")
("^(|not )clear the logs$")
Can I handle this in Java?
I want to perform action depending up on the key I pass
I found the solution, I have done it in Java as below
#Then("^verify word does(not|) exists in log$")
public void verifyLogs(String action) {
switch statement
code logic
//or
if(action.contains("not")){
code logic
}
}
Here is the solution in Ruby, it might give you an idea how to Updat your step and step def.
Step:
And verify word does exist in log
And verify word does not exist in log
Step Def:
And(/^verify word does( not)? exist in log$/) do |negative|
if negative
# negative logic
else
# positive logic
end
end
Based on your examples, I'm assuming you have two different options ("does" or "does not").
You have several options of capturing this in your step definition, for instance
using capture groups:
#Then("^verify word (does|does not) exist in log$")
using regex:
#Then("^verify word (.*) exist in log$")
using Cucumber expressions:
#Then("verify word {string} exist in log")
Next, you'll have to implement the step definition, to do something different depending on whether or not the String you passed contains "not".
public void verifyLogs(String shouldContain) {
if(action.contains("not")){
// call helper method to verify logs **do not** contain the word
}
// call helper method to verify logs **do** contain the word
}
Alternatively, you could use two different step definitions:
#Then("^verify word does exist in log$")
public void verifyLogs(String shouldContain) {
// call helper method to verify logs do contain the word
}
and
#Then("^verify word does not exist in log$")
public void verifyLogs(String shouldContain) {
// call helper method to verify logs **do not** contain the word
}
The upside of the last alternative is that your step definitions will stay very simple / don't contain any logic. The downside ofcourse is you have 2 step definitions, which is not a very big downside imho.
I am wondering what is the most effective form of data validation for android. So, when getting a value from an EditText how should the value given be validated before it is used? I currently check to make sure the string returned from getText().toString() is not null or empty using the guava library:
Strings.isNullOrEmpty(editText.getText().toString())
Then, depending on what type of data I am expecting, I create a method to see if the data can be parsed. So if I am expecting a double I will create a method like this:
private boolean isDouble(String string) {
try {
double stringDouble = Double.parseDouble(string);
return true;
} catch (Exception e)
{
return false;
}
}
Is there a simpler way to do this without the need to create a separate method for each type of data I am expecting to receive?
I've used the edittext-validator the last time I needed a quick validation. Works like charm :)
https://github.com/vekexasia/android-edittext-validator
If you are using EditText you should know what purposes it serves. Usually you are expecting to have just a plain string to, for example, send it to server or store in SharedPreferences.
But, if you want a particular data to be filled in, you can use Pattern class for validation. In case of number values you are using android:inputType="number" and when converting it using Integer.valueOf(text)
I have the following REGEX that I'm serving up to java via an xml file.
[a-zA-Z -\(\) \-]+
This regex is used to validate server side and client side (via javascript) and works pretty well at allowing only alphabetic content and a few other characters...
My problem is that it will also allow zero lenth strings / empty through.
Does anyone have a simple and yet elegant solution to this?
I already tried...
[a-zA-Z -\(\) \-]{1,}+
but that didn;t seem to work.
Cheers!
UPDATE FOLLOWING INVESTIGATION
It appears the code I provided does in fact work...
String inputStr = " ";
String pattern = "[a-zA-Z -\\(\\) \\-]+";
boolean patternMatched = java.util.regex.Pattern.matches(pattern, inputStr);
if ( patternMatched ){
out.println("Pattern MATCHED");
}else{
out.println("NOT MATCHED");
}
After looking at this more closely I think the problem may well be within the logic of some of my java bean coding... It appears the regex is dropped out at the point where the string parse should take place, thereby allowing empty strings to be submitted... And also any other string... EEJIT that I am...
Cheers for the help in peer reviewing my initial stupid though....!
Have you tried this:
[a-zA-Z -\(\) \-]+