Regular expression not working inside string - regex

I need to validate JSON through cucumber and this JSON is generated run time. JSON is like below.
{
"k1":["A", "B1234"]
}
Here 1234 is any random number appended with B.
I am using below template to validate any JSON like above-
{
"k1":["A", "B[0-9]+"]
}
But this is not working. I am getting message like Unable to validate JSON.
Any suggestion is highly appreciated.
Thanks

Use this
{
"k1":["A", "B\[0\-9\]\+"]
}
it should work.

Related

how to validate email in javafx TextField?

GOAL:
I'm trying to validate the user's email address as he/she types.
I'm just using simple regex because I don't want to use any external library. Also if use the regex like in here regex email for java I'm encountering error PatternSyntaxException that's why I prefer simple regex for now.
So I put the TextFormatter and UnaryOperator inside the initialize method.
#FXML
public void initialize()
{
//1. USE UNARY FIRST TO MAKE FILTER BEFORE USING TEXTFORMATTER
UnaryOperator <TextFormatter.Change> filterEmail = (change ->{
if(change.getControlNewText().matches("^(.+)#(.+)$*"))
{
lblEmailError.setVisible(false);
txtEmailAdd.setBorder(null);
return change;
}
else
{
lblEmailError.setText("Invalid Email");
lblEmailError.setVisible(true);
txtEmailAdd.setBorder(new Border(new BorderStroke(Color.RED, BorderStrokeStyle.SOLID, new CornerRadii(3), new BorderWidths(2), new Insets(-2))));
return null;
}
});
TextFormatter <String> tf = new TextFormatter<String>(filterEmail);
txtEmailAdd.setTextFormatter(tf);
}
But as long as the FXML loads I can't type anymore in the TextField and can't be edited anymore. I can't type anything. Maybe there is something wrong with my condition or I'm wrong putting it inside the initialize method.
I'm lost. I have already dug the web on how to validate email in java using regex.
like this Java regex email
I'm also using SceneBuilder to build the fxml. Any help will do thanks in advance.

Using Postman, unable to reference CSV file for JavaScript test

I am using a REST API with a POST request. I have created a CSV file to load in various inputs and using the Collection Runner to submit my requests and run the associated JavaScript Tests iteratively. I am trying to figure out how I can also have an entry in each row of the CSV to reference for my JavaScript Test in order to make the JavaScript dynamic. I've searched the Postman documentation and forums, as well as Google and Stack Overflow, but I haven't found anything that works. Here is a basic example of what I'm trying to accomplish.
Let's say I have a basic adding API. Here is my Request:
{
"Numbers": {
"Value_1": {{val1}},
"Value_2": {{val2}},
}
}
The CSV file is as follows:
val1,val2,sum
1,1,2
2,2,4
3,3,6
For this example, lets assume that the API returns a response that includes the sum of val1 and val2; something like this:
{
"Numbers": {{sum}},
}
I am able to load val1 and val2 into my request and iterate through the request for each row, but I am having trouble incorporating the sum values (from the same CSV) into the JavaScript Test.
I am trying to do something like the test below where I can reference the sum value from my spreadsheet, but Postman doesn't like my syntax.
pm.test("Adding machine", function () {
var jsonData = pm.response.json();
pm.expect(jsonData.Numbers === {{sum}});
});
Does anyone have any suggestions? Is this even possible to do?
You could use the pm.iterationData().get('var_name') function and create a check like this?
pm.test("Sums are correctly calculated", () => {
pm.expect(pm.response.json().Numbers).to.equal(pm.iterationData.get('sum'))
})

How to match specific string in ROBOT FRAMEWORK using regex?

I am using REST-API for testing
I am stuck where I am checking the response with some specific string.
please refer below info
I got the response from a request is
{
"clusters":[
{
"id":10,
"name":"HP2",
"status":2,
"statusDisplay":"HParihar#4info.com",
"lastModifiedBy":"HParihar#4info.com",
"lastModifiedTime":"06/08/2017 23:42",
"sitesAppsCount":0
},
{
"id":799,
"name":"Regression_cluster_111_09",
"status":2,
"statusDisplay":"admin#4info.net",
"lastModifiedBy":"admin#4info.net",
"lastModifiedTime":"07/11/2017 08:19",
"sitesAppsCount":0
}
]}
and I wanted to match just
"name":"Regression_cluster_111_09",
"status":2,
"statusDisplay":"admin#4info.net",
"sitesAppsCount":0
right side values I'll be keeping as hard coded.
any guesses?
Since you are only checking those 4 parameters are in response or not.
Do no use regex for this.
Use jsonObject's find key/value feature.
Check whether the values to the keys are there.
If key/value is null, the parameter is not in response.
I got my answer
I used the following regex
"name":"Regression_cluster_111_09","status":2,"statusDisplay":"admin#4info.net","lastModifiedBy":"[a-z]+#[0-9a-z]+\.[a-z]+","lastModifiedTime":"[0-9]{2}\/[0-9]{2}\/[0-9]{4}\ [0-9]{2}:[0-9]{2}","sitesAppsCount":0
or I can simply use
"name":"Regression_cluster_111_09","status":2,"statusDisplay":"admin#4info.net",.+"sitesAppsCount":0
thank you all

Jmter complicated regular expression solution? [duplicate]

I have following JSON format in response body
[
{
"Name" : "Prashant",
"City" : "Sydney"
},
{
"Name" : "Yogi",
"City" : "London"
}
]
What is the better way for checking if this array has any records and if yes give me "Name" for first array index. I am using jp#gc JSON extractor plugin for jMeter.
Is it possible to parse this using a plugin or do I need to do it using regular expressions?
Using Ubik Load Pack JSON plugin for JMeter which is part of JMeter since version 3.0 (donated plugin) and called JSON Extractor, you can do it:
Test Plan overview:
ULP_JSON PostProcessor:
If Controller:
And here is the run result:
So as you can see it is possible with plain JMeter
If you're looking to learn JMeter, this book by 3 developers of the project will help you.
I am not sure about your plugin but if it supports JSON path expressions it should be possible.
Try with this expression: $.[0].Name.
This is the plugin I use: http://jmeter-plugins.org/wiki/JSONPathExtractor/ and given expression works with it.
You can find more about JSON Path expressions here: http://goessner.net/articles/JsonPath/index.html#e2.
Working with JSON in JMeter is not quite easy as JMeter was designed long ago before JSON was invented.
There are some extensions however that make life easier:
http://www.ubik-ingenierie.com/blog/extract-json-content-efficiently-with-jmeter-using-json-path-syntax-with-ubik-load-pack/
We can add a regular expression extractor for fetching the value from the response.
Like This:
If possible, always use Regular Expression Extractor. Try to avoid JSON / XPATH / Other extractors. They might look easy to use. But they consume more memory and time. It will affect the performance of your test plan.
source: http://www.testautomationguru.com/jmeter-response-data-extractors-comparison/
Rest Get service sample:
{
"ObjectIdentifiers": {
"internal": 1,
"External1": "221212-12121",
"External3": "",
"Name": "koh"
},
"PartyType": "naturalPerson",
"NaturalPerson": {
"idNo": "221212-12121",
"Title": "Mr",
"Name": "koh",
"FirstName": "",
We had a similar requirement in our project for parsing json responses using jmeter. The requirement was to validate all the fields in the json response and the expected values of field would be provided from external data source.
I found the JSR223 PostProcessor quite usefule in this case as we are able to implement Groovy scripts with this. it comes as a default plugin with the recent Jmeter version
Edit:
Below is the code snippet:
//get the JSON response from prev sampler
String getResponse = prev.getResponseDataAsString();
//parse the response and convert to string
JSONParser parser = new JSONParser(JSONParser.MODE_JSON_SIMPLE);
String parResponse = parser.parse(getResponse);
String preResponse = parResponse.toString();
JsonObject NaturalPerson = JsonObject.readFrom(preResponse);
//replace all commas with a semi-colon
String csvResponse = preResponse.replaceAll(",", ";");
//log response to file
logFileName = "C:/apache-jmeter-5.1.1/Web_Service_Output.csv";
BufferedWriter outLog = new BufferedWriter(new FileWriter(logFileName, true));
outLog.write(csvResponse + "\n");
outLog.close();

GWT TextBox Regex

I am using GWT textBox and i want it to allow only alpha numeric characters.Is there any example somewhere where i can use the RegExp which is provided in GWT 2.1?
I would like to apply a regex for example ^[a-zA-Z0-9_]*$ .Please provide example code for making this working.Also i cannot use a third party library like the GWT-VL or something like that.
I am using GWT 2.1.
Thanks
You can simply use the matches method of the class String.
Here an example of an email validation:
if(!email.matches("^([a-zA-Z0-9_.\\-+])+#(([a-zA-Z0-9\\-])+\\.)+[a-zA-Z0-9]{2,4}$")) {
setErrorText("wrong mail");
}
Note that email is a String object.
HI you can use GWT-Validator framnework to do this GWT-Validator
Or you can use javascript method through JSNI and the method is mentioned below.
function validate(var string){
var alphanum=/^[0-9a-bA-B]+$/; //This contains A to Z , 0 to 9 and A to B
if(string.match(alphanum)){
return true;
}else{
alert(Put a Valid Userid Name);
return false;
}
}