How to parser text into string arrays

Hi all,
New to Katalon Studio and would like to know what function calls that can be used to parser all numbers in double quotation into string arrays
Example how to get string sNumbers[]= “0563300111”, “0563300127” and …
from below structure
var TestMap={Test1:{Item:“Name”,Number:0},Test2:{Item:“Name1”,Number:21},“0563300111”:{Item:“NameP”,Number:0},“0563300127”:{Item:“NameP”,Number:21},“0563300154”:{Item:“NameP”,Number:22},“0563300166”:{Item:“NameP”,Number:22},“0563300142”:{Item:“NameP”,Number:99},“0563300131”:{Item:“Name2”,Number:18},“0563300151”:{Item:“Name2”,Number:18},“0563300163”:{Item:“Name2”,Number:0},“0563300136”:{Item:“Name3”,Number:0},“0563300143”:{Item:“Name3”,Number:0},“0563300152”:{Item:“Name3”,Number:21},“0563300164”:{Item:“Name3”,Number:22},“0563300147”:{Item:“Name3”,Number:99},“0563300139”:{Item:“pe”,Number:6},“0563300170”:{Item:“pe”,Number:7},“0563300172”:{Item:“pe”,Number:7},“0563300173”:{Item:“pe”,Number:7},“0563300137”:{Item:“Name”,Number:0},“0563300144”:{Item:“Name”,Number:0},“0563300145”:{Item:“Name”,Number:0},“0563300146”:{Item:“Name”,Number:0},“0563300148”:{Item:“Name”,Number:0},“0563300165”:{Item:“Name”,Number:21},“0563300156”:{Item:“Name”,Number:21},“0563300157”:{Item:“Name”,Number:21},“0563300158”:{Item:“Name”,Number:21},“0563300159”:{Item:“Name”,Number:21},“0563300160”:{Item:“Name”,Number:21},“0563300153”:{Item:“Name mac”,Number:21},“0563300161”:{Item:“Name mac”,Number:21},“0563300168”:{Item:“Name beta”,Number:21},“0563300169”:{Item:“Name mac beta”,Number:21},“0563300140”:{Item:“Name1”,Number:21},“0563300162”:{Item:“Name1”,Number:21},“0563300167”:{Item:“Name1”,Number:21},“0563300149”:{Item:“Name8”,Number:1},“0563300150”:{Item:“Name8”,Number:99}};
Thanks

hi, 
this is quick & dirty way :slight_smile: 

import java.util.regex.Matcher;
import java.util.regex.Pattern;

Pattern p = Pattern.compile("\\d+");

Matcher m = p.matcher('{Test1:{Item:"Name",Number:0},Test2:{Item:"Name1",Number:21},"0563300111":{Item:"NameP",Number:0},"0563300127":{Item:"NameP",Number:21},"0563300154":{Item:"NameP",Number:22},"0563300166":{Item:"NameP",Number:22},"0563300142":{Item:"NameP",Number:99},"0563300131":{Item:"Name2",Number:18},"0563300151":{Item:"Name2",Number:18},"0563300163":{Item:"Name2",Number:0},"0563300136":{Item:"Name3",Number:0},"0563300143":{Item:"Name3",Number:0},"0563300152":{Item:"Name3",Number:21},"0563300164":{Item:"Name3",Number:22},"0563300147":{Item:"Name3",Number:99},"0563300139":{Item:"pe",Number:6},"0563300170":{Item:"pe",Number:7},"0563300172":{Item:"pe",Number:7},"0563300173":{Item:"pe",Number:7},"0563300137":{Item:"Name",Number:0},"0563300144":{Item:"Name",Number:0},"0563300145":{Item:"Name",Number:0},"0563300146":{Item:"Name",Number:0},"0563300148":{Item:"Name",Number:0},"0563300165":{Item:"Name",Number:21},"0563300156":{Item:"Name",Number:21},"0563300157":{Item:"Name",Number:21},"0563300158":{Item:"Name",Number:21},"0563300159":{Item:"Name",Number:21},"0563300160":{Item:"Name",Number:21},"0563300153":{Item:"Name mac",Number:21},"0563300161":{Item:"Name mac",Number:21},"0563300168":{Item:"Name beta",Number:21},"0563300169":{Item:"Name mac beta",Number:21},"0563300140":{Item:"Name1",Number:21},"0563300162":{Item:"Name1",Number:21},"0563300167":{Item:"Name1",Number:21},"0563300149":{Item:"Name8",Number:1},"0563300150":{Item:"Name8",Number:99}};')
List<String> list = new ArrayList<>()

while(m.find()) {
	def num = m.group()
	if (num.length() > 9){
		list.add(num)
	}
}
println list

[0563300111, 0563300127, 0563300154, 0563300166, 0563300142, 0563300131, 0563300151, 0563300163, 0563300136, 0563300143, 0563300152, 0563300164, 0563300147, 0563300139, 0563300170, 0563300172, 0563300173, 0563300137, 0563300144, 0563300145, 0563300146, 0563300148, 0563300165, 0563300156, 0563300157, 0563300158, 0563300159, 0563300160, 0563300153, 0563300161, 0563300168, 0563300169, 0563300140, 0563300162, 0563300167, 0563300149, 0563300150]

Hi @Timo_Kuisma1,
Thanks for your response and looks like you are using Java to save the list so try to find the duplicate in the list but failed with Groovy code. Can you let me know what have I done wrong?
Thanks

Here is the code

if (isDuplicate(list))
println (“Duplicated Found”)
else
println (“Duplicated Not Found”)

private boolean isDuplicate(List arrayList) {
for (int i = 0; i < arrayList.size(); i++) {
for (int j = i + 1; j < arrayList.size(); j++) {
if (arrayList.get(i).getName().trim().equalsIgnoreCase(arrayList.get(j).getName().trim())) {
return true;
}
}
}

return false;

}

not sure why will get below groovy exception error
Reason:
groovy.lang.MissingMethodException: No signature of method: java.lang.String.getName() is applicable for argument types: () values: []
Possible solutions: getAt(groovy.lang.IntRange), getAt(groovy.lang.IntRange), getAt(int), getAt(java.util.Collection), getAt(groovy.lang.Range), getAt(java.lang.String)
at Test Case 16 - Duplicate AppID Check.isDuplicate(Test Case 16 - Duplicate AppID Check:25)
at Test Case 16 - Duplicate AppID Check.run(Test Case 16 - Duplicate AppID Check:52)
at com.kms.katalon.core.main.ScriptEngine.run(ScriptEngine.java:194)
at com.kms.katalon.core.main.ScriptEngine.runScriptAsRawText(ScriptEngine.java:119)
at com.kms.katalon.core.main.TestCaseExecutor.runScript(TestCaseExecutor.java:337)
at com.kms.katalon.core.main.TestCaseExecutor.doExecute(TestCaseExecutor.java:328)
at com.kms.katalon.core.main.TestCaseExecutor.processExecutionPhase(TestCaseExecutor.java:307)
at com.kms.katalon.core.main.TestCaseExecutor.accessMainPhase(TestCaseExecutor.java:299)
at com.kms.katalon.core.main.TestCaseExecutor.execute(TestCaseExecutor.java:233)
at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:114)
at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:105)
at com.kms.katalon.core.main.TestCaseMain$runTestCase$0.call(Unknown Source)
at TempTestCase1581585299149.run(TempTestCase1581585299149.groovy:23)

hi,
done by IntelliJ

package regex;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegEx {

    public static List<String> testRegEx() {
        Pattern p = Pattern.compile("\\d+");
        String inputStr = "{Test1:0563300111{Item:\"Name\",Number:0},Test2:{Item:\"Name1\",Number:21},\"0563300111\":{Item:\"NameP\",Number:0},\"0563300127\":{Item:\"NameP\",Number:21},\"0563300154\":{Item:\"NameP\",Number:22},\"0563300166\":{Item:\"NameP\",Number:22},\"0563300142\":{Item:\"NameP\",Number:99},\"0563300131\":{Item:\"Name2\",Number:18},\"0563300151\":{Item:\"Name2\",Number:18},\"0563300163\":{Item:\"Name2\",Number:0},\"0563300136\":{Item:\"Name3\",Number:0},\"0563300143\":{Item:\"Name3\",Number:0},\"0563300152\":{Item:\"Name3\",Number:21},\"0563300164\":{Item:\"Name3\",Number:22},\"0563300147\":{Item:\"Name3\",Number:99},\"0563300139\":{Item:\"pe\",Number:6},\"0563300170\":{Item:\"pe\",Number:7},\"0563300172\":{Item:\"pe\",Number:7},\"0563300173\":{Item:\"pe\",Number:7},\"0563300137\":{Item:\"Name\",Number:0},\"0563300144\":{Item:\"Name\",Number:0},\"0563300145\":{Item:\"Name\",Number:0},\"0563300146\":{Item:\"Name\",Number:0},\"0563300148\":{Item:\"Name\",Number:0},\"0563300165\":{Item:\"Name\",Number:21},\"0563300156\":{Item:\"Name\",Number:21},\"0563300157\":{Item:\"Name\",Number:21},\"0563300158\":{Item:\"Name\",Number:21},\"0563300159\":{Item:\"Name\",Number:21},\"0563300160\":{Item:\"Name\",Number:21},\"0563300153\":{Item:\"Name mac\",Number:21},\"0563300161\":{Item:\"Name mac\",Number:21},\"0563300168\":{Item:\"Name beta\",Number:21},\"0563300169\":{Item:\"Name mac beta\",Number:21},\"0563300140\":{Item:\"Name1\",Number:21},\"0563300162\":{Item:\"Name1\",Number:21},\"0563300167\":{Item:\"Name1\",Number:21},\"0563300149\":{Item:\"Name8\",Number:1},\"0563300150\":{Item:\"Name8\",Number:99}};)";

        Matcher m = p.matcher(inputStr);
        List<String> list = new ArrayList<>();

        while (m.find()) {
            String num = m.group();
            if (num.length() > 9) {
                list.add(num);
            }
        }
        System.out.println(list);
        return list;

    }

    public static Set<String> searchDublicates(List<String> list){
        final Set<String> setToReturn = new HashSet<String>();
        final Set<String> set1 = new HashSet<String>();

        for (String s : list) {
            if (!set1.add(s)) {
                setToReturn.add(s);
            }
        }
        return setToReturn;
    }

    public static void main(String[] args){
        List<String> list = new ArrayList<>();
        Set<String> dup = new HashSet<String>();
        list = testRegEx();
        dup = searchDublicates(list);
        System.out.println("DEBUG duplicate item "+dup);
    }
}

[0563300111, 0563300111, 0563300127, 0563300154, 0563300166, 0563300142, 0563300131, 0563300151, 0563300163, 0563300136, 0563300143, 0563300152, 0563300164, 0563300147, 0563300139, 0563300170, 0563300172, 0563300173, 0563300137, 0563300144, 0563300145, 0563300146, 0563300148, 0563300165, 0563300156, 0563300157, 0563300158, 0563300159, 0563300160, 0563300153, 0563300161, 0563300168, 0563300169, 0563300140, 0563300162, 0563300167, 0563300149, 0563300150]

DEBUG duplicate item [0563300111]

Hi @Timo_Kuisma1,
Thanks for rewriting the code to a class. Sorry that I am new with the tool and the problem here is that how can I get the string sCapturedText created in Katalon Variables

<?xml version="1.0" encoding="UTF-8"?> '' 1b48d49d-8fd6-4bc1-93e1-4aea925c3d33 false sCapturedText

with value captured from WebUI.getText to pass to the inputStr in your Regex class?
sCapturedText = WebUI.getText(findTestObject(‘Object Repository/pre_var productMapAutomationTests_Product1p_b6ce40’))
Thanks

hi,

didn’t got your point, could you describe little bit more

Hi @Timo_Kuisma1,
The string sCapturedText = WebUI.getText(findTestObject(‘Object Repository/pre_var productMapAutomationTests_Product1p_b6ce40’)) will contain dynamic structure similar to
static String inputStr that you have created in class RegEx

String inputStr ="{Test1:0563300111{Item:“Name”,Number:0},Test2:{Item:“Name1”,Number:21},“0563300111”:{Item:“NameP”,Number:0},“0563300127”:{Item:“NameP”,Number:21},“0563300154”:{Item:“NameP”,Number:22},“0563300166”:{Item:“NameP”,Number:22},“0563300142”:{Item:“NameP”,Number:99},“0563300131”:{Item:“Name2”,Number:18},“0563300151”:{Item:“Name2”,Number:18},“0563300163”:{Item:“Name2”,Number:0},“0563300136”:{Item:“Name3”,Number:0},“0563300143”:{Item:“Name3”,Number:0},“0563300152”:{Item:“Name3”,Number:21},“0563300164”:{Item:“Name3”,Number:22},“0563300147”:{Item:“Name3”,Number:99},“0563300139”:{Item:“pe”,Number:6},“0563300170”:{Item:“pe”,Number:7},“0563300172”:{Item:“pe”,Number:7},“0563300173”:{Item:“pe”,Number:7},“0563300137”:{Item:“Name”,Number:0},“0563300144”:{Item:“Name”,Number:0},“0563300145”:{Item:“Name”,Number:0},“0563300146”:{Item:“Name”,Number:0},“0563300148”:{Item:“Name”,Number:0},“0563300165”:{Item:“Name”,Number:21},“0563300156”:{Item:“Name”,Number:21},“0563300157”:{Item:“Name”,Number:21},“0563300158”:{Item:“Name”,Number:21},“0563300159”:{Item:“Name”,Number:21},“0563300160”:{Item:“Name”,Number:21},“0563300153”:{Item:“Name mac”,Number:21},“0563300161”:{Item:“Name mac”,Number:21},“0563300168”:{Item:“Name beta”,Number:21},“0563300169”:{Item:“Name mac beta”,Number:21},“0563300140”:{Item:“Name1”,Number:21},“0563300162”:{Item:“Name1”,Number:21},“0563300167”:{Item:“Name1”,Number:21},“0563300149”:{Item:“Name8”,Number:1},“0563300150”:{Item:“Name8”,Number:99}};)";

Just need to pass in sCapturedText variable to class RegEx in order to check for duplicate string.
Hope above explanation is clear to you.
Thanks

hello,

All this in testcase

import java.sql.Timestamp
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI

import java.util.regex.Matcher;
import java.util.regex.Pattern;


List<String> li = new ArrayList<>();
Set<String> dup = new HashSet<String>();
String inputStr = "{Test1:0563300111{Item:\"Name\",Number:0},Test2:{Item:\"Name1\",Number:21},\"0563300111\":{Item:\"NameP\",Number:0},\"0563300127\":{Item:\"NameP\",Number:21},\"0563300154\":{Item:\"NameP\",Number:22},\"0563300166\":{Item:\"NameP\",Number:22},\"0563300142\":{Item:\"NameP\",Number:99},\"0563300131\":{Item:\"Name2\",Number:18},\"0563300151\":{Item:\"Name2\",Number:18},\"0563300163\":{Item:\"Name2\",Number:0},\"0563300136\":{Item:\"Name3\",Number:0},\"0563300143\":{Item:\"Name3\",Number:0},\"0563300152\":{Item:\"Name3\",Number:21},\"0563300164\":{Item:\"Name3\",Number:22},\"0563300147\":{Item:\"Name3\",Number:99},\"0563300139\":{Item:\"pe\",Number:6},\"0563300170\":{Item:\"pe\",Number:7},\"0563300172\":{Item:\"pe\",Number:7},\"0563300173\":{Item:\"pe\",Number:7},\"0563300137\":{Item:\"Name\",Number:0},\"0563300144\":{Item:\"Name\",Number:0},\"0563300145\":{Item:\"Name\",Number:0},\"0563300146\":{Item:\"Name\",Number:0},\"0563300148\":{Item:\"Name\",Number:0},\"0563300165\":{Item:\"Name\",Number:21},\"0563300156\":{Item:\"Name\",Number:21},\"0563300157\":{Item:\"Name\",Number:21},\"0563300158\":{Item:\"Name\",Number:21},\"0563300159\":{Item:\"Name\",Number:21},\"0563300160\":{Item:\"Name\",Number:21},\"0563300153\":{Item:\"Name mac\",Number:21},\"0563300161\":{Item:\"Name mac\",Number:21},\"0563300168\":{Item:\"Name beta\",Number:21},\"0563300169\":{Item:\"Name mac beta\",Number:21},\"0563300140\":{Item:\"Name1\",Number:21},\"0563300162\":{Item:\"Name1\",Number:21},\"0563300167\":{Item:\"Name1\",Number:21},\"0563300149\":{Item:\"Name8\",Number:1},\"0563300150\":{Item:\"Name8\",Number:99}};)";
li = testRegEx(inputStr);
dup = searchDublicates(li);
System.out.println("DEBUG duplicate item "+dup);

Pattern p = Pattern.compile("\\d+");

public static List<String> testRegEx(String inputStr) {
	Pattern p = Pattern.compile("\\d+");
	Matcher m = p.matcher(inputStr);
	List<String> list = new ArrayList<>();

	while (m.find()) {
		String num = m.group();
		if (num.length() > 9) {
			list.add(num);
		}
	}
	//System.out.println(list);
	return list;
}

public static Set<String> searchDublicates(List<String> list){
	final Set<String> setToReturn = new HashSet<String>();
	final Set<String> set1 = new HashSet<String>();

	for (String s : list) {
		if (!set1.add(s)) {
			setToReturn.add(s);
		}
	}
	return setToReturn;
}

Hi @Timo_Kuisma1,
Thank you very much and finally got it to work after two days

ParserText = WebUI.getText(findTestObject('Object Repository/IPP/IPP Web/Page_/pre_var productMapAutomationTests_Product1p_b6ce40'))

List<String> li = new ArrayList<>();
Set<String> dup = new HashSet<String>();
li = testRegEx(ParserText);
dup = searchDublicates(li);
System.out.println("DEBUG duplicate item "+dup);

public static List<String> testRegEx(String inputStr) {
	Pattern p = Pattern.compile("\\d+");
	Matcher m = p.matcher(inputStr);
	List<String> list = new ArrayList<>();

	while (m.find()) {
		String num = m.group();
		if (num.length() > 9) {
			list.add(num);
		}
	}
	list.add("0563300111")
	list.add("0563300127")
	System.out.println(list);
	return list;
}

public static Set<String> searchDublicates(List<String> list){
	final Set<String> setToReturn = new HashSet<String>();
	final Set<String> set1 = new HashSet<String>();

	for (String s : list) {
		if (!set1.add(s)) {
			setToReturn.add(s);
		}
	}
	return setToReturn;
}
WebUI.closeBrowser() 

with below output

2020-02-14 07:46:15.014 ERROR c.k.k.c.webui.common.WebUiCommonHelper - :x: [SMART_XPATH] C:\Users\Mike.Wang\git\CITA\Reports\smart_xpath\waiting-for-approval.json (The system cannot find the file specified)
2020-02-14 07:46:15.016 ERROR c.k.k.c.webui.common.WebUiCommonHelper - :x: [SMART_XPATH] C:/Users/Mike.Wang/git/CITA/Reports/smart_xpath/waiting-for-approval.json does not exist or is provided by Smart XPath Plugin!
2020-02-14 07:46:15.017 INFO c.k.k.c.webui.common.WebUiCommonHelper - [SMART_XPATH] Selected Smart XPath: '//*/text()[normalize-space(.)='var productMap={
2020-02-14 07:46:15.017 INFO c.k.k.c.webui.common.WebUiCommonHelper - [SMART_XPATH] Continuing executing the test case!
2020-02-14 07:46:15.052 DEBUG t.Test Case 16 - Duplicate AppID Check - 4: li = new java.util.ArrayList()
2020-02-14 07:46:15.052 DEBUG t.Test Case 16 - Duplicate AppID Check - 5: dup = new java.util.HashSet()
2020-02-14 07:46:15.068 DEBUG t.Test Case 16 - Duplicate AppID Check - 6: li = Script1581319422311.testRegEx(ParserText)
[0563300111, 0563300127, 0563300154, 0563300166, 0563300142, 0563300131, 0563300151, 0563300163, 0563300136, 0563300143, 0563300152, 0563300164, 0563300147, 0563300139, 0563300170, 0563300172, 0563300173, 0563300137, 0563300144, 0563300145, 0563300146, 0563300148, 0563300165, 0563300156, 0563300157, 0563300158, 0563300159, 0563300160, 0563300153, 0563300161, 0563300168, 0563300169, 0563300140, 0563300162, 0563300167, 0563300149, 0563300150, 0563300111, 0563300127]
2020-02-14 07:46:15.392 DEBUG t.Test Case 16 - Duplicate AppID Check - 7: dup = Script1581319422311.searchDublicates(li)
2020-02-14 07:46:15.412 DEBUG t.Test Case 16 - Duplicate AppID Check - 8: out.println(“DEBUG duplicate item " + dup)
DEBUG duplicate item [0563300127, 0563300111]
2020-02-14 07:46:15.412 DEBUG t.Test Case 16 - Duplicate AppID Check - 9: p = Pattern.compile(”\d+")
2020-02-14 07:46:15.412 DEBUG t.Test Case 16 - Duplicate AppID Check - 10: closeBrowser()

1 Like