in Education by
I want to find out if a given String 'b' matches the pattern of String 'a'. Furthermore, the String 'a' may contain placeholder/slots, while String 'b' contains the actual values, which should be extracted. Example: String a = "Hello my name is and I am from " String b = "Hello my name is Ben and I am from New York" Expected Results: -> b matches a -> NAME = "Ben" -> CITY = "New York" To determine if a and b matches, I proceed as follows: b.matches(a.replaceAll("<.*>", ".*")) But I currently have no clue how to implement this "slot"-extraction in a generic and robust way. I would be grateful for any recommendations/hins. JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
You can replace tokens like this with (.*) from your first string to form capture groups and then create a Pattern using the grouped string. Then you can use the second string to match the pattern and if it matches, then you can access all the groups to retrieve the data from groups. Here is the initial code which I think should work and can be updated as per your other needs to make it further robust. public static void main(String[] args) { matchAndExtract("Hello my name is and I am from ", "Hello my name is Ben and I am from New York"); } public static void matchAndExtract(String s1, String s2) { List placeHolderNames = new ArrayList<>(); Pattern p1 = Pattern.compile("(?<=<)[^<>]+(?=>)"); Matcher m1 = p1.matcher(s1); while (m1.find()) { placeHolderNames.add(m1.group()); } Pattern p2 = Pattern.compile(s1.replaceAll("<.*?>", "(.*)")); Matcher m2 = p2.matcher(s2); if (m2.matches()) { System.out.println("Both string matches"); for (int i = 0; i < m2.groupCount(); i++) { System.out.println(placeHolderNames.get(i) + " = " + m2.group(i + 1)); } } else { System.out.println("Both string doesn't match"); } } Prints, Both string matches NAME = Ben CITY = New York Let me know if this is what you were looking for and works for you.

Related questions

0 votes
    I want to find out if a given String 'b' matches the pattern of String 'a'. Furthermore, the ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 15, 2022 in Education by JackTerrance
0 votes
    I have a string like (&(objectclass=${abc})(uid=${xyz})) How can I search ${abc} and $ ... questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 13, 2022 in Education by JackTerrance
0 votes
    I have a string like (&(objectclass=${abc})(uid=${xyz})) How can I search ${abc} and $ ... questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 13, 2022 in Education by JackTerrance
0 votes
    I have a string like (&(objectclass=${abc})(uid=${xyz})) How can I search ${abc} and $ ... questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 13, 2022 in Education by JackTerrance
0 votes
    I have a string like (&(objectclass=${abc})(uid=${xyz})) How can I search ${abc} and $ ... questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 13, 2022 in Education by JackTerrance
0 votes
    IR (information Retrieval) and IE (Information Extraction) are the two same thing. (a) True (b) False ... and Acting of Artificial Intelligence Please answer the above question....
asked Oct 9, 2022 in Education by JackTerrance
0 votes
    IR (information Retrieval) and IE (Information Extraction) are the two same thing. (a) True (b) False ... and Acting of Artificial Intelligence Please answer the above question....
asked Oct 2, 2022 in Education by JackTerrance
0 votes
    Give a program in JAVA for word extraction . Very urgent please answer fast . Select the correct answer from above options...
asked Dec 13, 2021 in Education by JackTerrance
0 votes
    How to concatenate multiple rows into a single string in sql a. COALESCE b. STRING_AGG C. Option 1 & 2 d. None of the above...
asked Feb 19, 2023 in Technology by JackTerrance
0 votes
    I am trying to convert string into integer using jquery but it throwing NumberFormatException. How to fix it ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 20, 2022 in Education by JackTerrance
0 votes
    I'm working in an FFI library and have encountered this pattern quite a few times that I don't ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 20, 2022 in Education by JackTerrance
0 votes
    I am a newbie to Scala and I want to learn how can I add null and empty check on an optional ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 6, 2022 in Education by JackTerrance
0 votes
    I am a newbie to Scala and I want to learn how can I add null and empty check on an optional ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 26, 2022 in Education by JackTerrance
0 votes
    I need a way to remove the first character from a string which is a space. I am looking for a ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 23, 2022 in Education by JackTerrance
0 votes
    Me and my colleague have different versions of VisualStudio. He used interpolated string and I couldn't ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 19, 2022 in Education by JackTerrance
...