Tuesday, 1 July 2025

𝗦𝘁𝗶𝗹𝗹 𝘀𝘁𝗿𝘂𝗴𝗴𝗹𝗶𝗻𝗴 𝘄𝗶𝘁𝗵 𝗝𝗮𝘃𝗮 𝗰𝗼𝗱𝗶𝗻𝗴 𝗿𝗼𝘂𝗻𝗱𝘀?




❓ 𝗦𝘁𝗶𝗹𝗹 𝘀𝘁𝗿𝘂𝗴𝗴𝗹𝗶𝗻𝗴 𝘄𝗶𝘁𝗵 𝗝𝗮𝘃𝗮 𝗰𝗼𝗱𝗶𝗻𝗴 𝗿𝗼𝘂𝗻𝗱𝘀?

The issue isn’t your logic—it’s the lack of pattern recognition. As an SDET, knowing when to apply which pattern is the game-changer.


-x-x-x-x
👉 Refer for Quick Interview Revision: https://lnkd.in/ghvNBFPU
👉 𝗦𝘁𝗮𝗿𝘁 𝘆𝗼𝘂𝗿 end to end 𝗔𝘂𝘁𝗼𝗺𝗮𝘁𝗶𝗼𝗻 𝗹𝗲𝗮𝗿𝗻𝗶𝗻𝗴 𝗷𝗼𝘂𝗿𝗻𝗲𝘆 𝗻𝗼𝘄 with 1:1 guidance and help in coding interview preparation: https://lnkd.in/giCxnJJ7
-x-x-x-


👉 𝗖𝗼𝗹𝗹𝗲𝗰𝘁𝗶𝗼𝗻𝘀 & 𝗠𝗮𝗽𝘀 – 𝗧𝗵𝗲 𝗖𝗼𝗿𝗲 𝗼𝗳 𝗔𝘂𝘁𝗼𝗺𝗮𝘁𝗶𝗼𝗻 𝗗𝗮𝘁𝗮 𝗛𝗮𝗻𝗱𝗹𝗶𝗻𝗴

When to use: Working with test data, mapping request/response pairs, or storing UI elements.

Example:

Map locators = new HashMap<>();

locators.put("loginBtn", "//button[text()='Login']");

Coding Problem Example:

Count frequency of characters or words → Use HashMap.

👉 𝗧𝘄𝗼 𝗣𝗼𝗶𝗻𝘁𝗲𝗿𝘀 – 𝗙𝗼𝗿 𝗦𝘁𝗿𝗶𝗻𝗴𝘀, 𝗔𝗿𝗿𝗮𝘆s

When to use: Useful in validations like palindromes, comparing logs, or merging sorted logs.

Example:

int i = 0, j = arr.length - 1;

while (i < j) {

  if (arr[i] != arr[j]) return false;

  i++; j--;

}

Coding Problem Example:

Is string a palindrome? Find pairs with given sum.

👉 𝗦𝗹𝗶𝗱𝗶𝗻𝗴 𝗪𝗶𝗻𝗱𝗼𝘄 – 𝗙𝗼𝗿 𝗦𝘂𝗯𝘀𝘁𝗿𝗶𝗻𝗴𝘀 𝗮𝗻𝗱 𝗣𝗲𝗿𝗳𝗼𝗿𝗺𝗮𝗻𝗰𝗲 𝗪𝗶𝗻𝗱𝗼𝘄𝘀

When to use: Identify performance drops, memory leaks, or longest continuous pass/fail patterns in logs.

Example:

Find the longest substring with no repeating characters.

👉 𝗦𝘁𝗿𝗶𝗻𝗴 𝗠𝗮𝗻𝗶𝗽𝘂𝗹𝗮𝘁𝗶𝗼𝗻 – 𝗙𝗼𝗿 𝗖𝗹𝗲𝗮𝗻𝘂𝗽𝘀, 𝗣𝗮𝗿𝘀𝗶𝗻𝗴, 𝗮𝗻𝗱 𝗙𝗼𝗿𝗺𝗮𝘁𝘁𝗶𝗻𝗴

When to use:When cleaning API responses, generating dynamic test data, or parsing HTML/XML/text logs.

Example:

String response = "status=200&message=Success";

String[] parts = response.split("&");

for (String part : parts) {

  String[] keyValue = part.split("=");

  System.out.println(keyValue[0] + ": " + keyValue[1]);

}

Coding Problem Example:

Reverse a string, remove duplicates, extract digits from test input, etc.

👉 𝗧𝗿𝘆-𝗖𝗮𝘁𝗰𝗵 – 𝗙𝗼𝗿 𝗦𝗮𝗳𝗲 𝗔𝘂𝘁𝗼𝗺𝗮𝘁𝗶𝗼𝗻 𝗖𝗼𝗱𝗲

When to use:Handling exceptions during test execution, parsing, or when dealing with unstable inputs like API responses, browser states, or external systems.

Example:

Handle potential exceptions during array access or parsing

try {

  int result = Integer.parseInt("123a"); // Will throw NumberFormatException

  System.out.println("Parsed number: " + result);

} catch (NumberFormatException e) {

  System.err.println("Invalid number format: " + e.getMessage());

}

👉 𝗥𝗲𝗴𝗲𝘅 & 𝗣𝗮𝘁𝘁𝗲𝗿𝗻 𝗠𝗮𝘁𝗰𝗵𝗶𝗻𝗴 – 𝗙𝗼𝗿 𝗟𝗼𝗴 𝗙𝗶𝗹𝗲 𝗩𝗮𝗹𝗶𝗱𝗮𝘁𝗶𝗼𝗻

Extract timestamps, validate test output format, match dynamic URLs.

Example:

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

Matcher m = p.matcher(logLine);

if (m.find()) Sysout("Error Code: " + m.group(1));

-x-x-x-

📌 Watch Mock Interviews here: http://youtube.com/@automatewithsidharth 

#sidpost #qa #sdet #career #automation 

Saturday, 5 April 2025

Java Regex Tutorial (With Examples)

 

Java Regex Tutorial (With Examples)

✅ What is Regex?

Regex (short for Regular Expression) is a sequence of characters that defines a search pattern. It is widely used for string pattern matching, validations, and data parsing.

In Java, regex is supported by the java.util.regex package.


Key Classes in Java Regex

  • Pattern: A compiled representation of a regular expression.

  • Matcher: An engine that performs match operations on a character sequence using a Pattern.


Basic Example



import java.util.regex.*;


public class RegexDemo {

    public static void main(String[] args) {

        Pattern pattern = Pattern.compile("Java");

        Matcher matcher = pattern.matcher("Java is fun");


        boolean matchFound = matcher.find();

        System.out.println("Match found? " + matchFound);  // Output: true

    }

}






Common Regex Patterns

Pattern

Meaning

Example Match

.

Any character

"a", "1", "#"

\d

Any digit (0-9)

"3", "9"

\w

Any word char (a-z, A-Z, 0-9, _)

"a", "Z", "3", "_"

\s

Whitespace

" " (space), \t

*

0 or more

"a*", "", "aaa"

+

1 or more

"a+", "aaa"

?

0 or 1

"a?", "", "a"

[]

Character set

"[aeiou]" matches any vowel

^

Starts with

"^Java" matches "Java is..."

$

Ends with

"end$" matches "The end"


Examples

✅ 1. Check if a string contains digits



String input = "My phone number is 12345";

boolean hasDigits = input.matches(".*\\d+.*");

System.out.println(hasDigits); // true



✅ 2. Validate email address



String email = "test@example.com";

boolean isValid = email.matches("^[\\w.-]+@[\\w.-]+\\.[a-zA-Z]{2,6}$");

System.out.println(isValid); // true



✅ 3. Extract numbers from a string



String input = "Order123, Invoice456";

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

Matcher matcher = pattern.matcher(input);


while (matcher.find()) {

    System.out.println("Found number: " + matcher.group());

}



✅ 4. Replace all whitespaces



String messy = "Java   is \t awesome!";

String cleaned = messy.replaceAll("\\s+", " ");

System.out.println(cleaned); // Java is awesome!



✅ 5. Validate phone number (e.g., US format)



String phone = "123-456-7890";

boolean isValid = phone.matches("\\d{3}-\\d{3}-\\d{4}");

System.out.println(isValid); // true



Bonus: Pattern Flags

You can make patterns case-insensitive:



Pattern pattern = Pattern.compile("java", Pattern.CASE_INSENSITIVE);

Matcher matcher = pattern.matcher("I love Java");

System.out.println(matcher.find()); // true



Tips for Automation Testers:

  • Use regex in assertions with API responses.

  • Clean dynamic values from logs before validation.

  • Validate formats (email, timestamp, ID) using .matches().


Practice Exercise for You:

  1. Write a regex to validate dates in format dd/mm/yyyy.

  2. Extract all words starting with capital letters from a paragraph.

  3. Replace all HTML tags with an empty string (basic HTML cleanup).

All Time Popular Posts

Most Featured Post

𝗦𝘁𝗶𝗹𝗹 𝘀𝘁𝗿𝘂𝗴𝗴𝗹𝗶𝗻𝗴 𝘄𝗶𝘁𝗵 𝗝𝗮𝘃𝗮 𝗰𝗼𝗱𝗶𝗻𝗴 𝗿𝗼𝘂𝗻𝗱𝘀?

❓ 𝗦𝘁𝗶𝗹𝗹 𝘀𝘁𝗿𝘂𝗴𝗴𝗹𝗶𝗻𝗴 𝘄𝗶𝘁𝗵 𝗝𝗮𝘃𝗮 𝗰𝗼𝗱𝗶𝗻𝗴 𝗿𝗼𝘂𝗻𝗱𝘀? The issue isn’t your logic—it’s the lack of pattern recogniti...