Create a regular expression to filter a specific data set.
here is the example
Write a regular expression that can represent any binary byte value, that is, that is, any eight-bit number with or without spaces in between each nibble. Valid input tests: 10100001 and 1010 0001 Invalid input tests: 10a0b0c0 and 00000000000000000000
This is one possible regular expression for representing a binary byte value: [01]{4}[ ]?[01]{4}.
In addition to the description, provide two test cases that will pass the input and one that will fail
So pretty much creating the specific topic using the regular expression and to filter specific data set.
The topic of the set can be about anything /Use JAVA for the code plz thank you.
The regex can be defined as: \b[01]{8}\b
This code will determine if the string is accepted or not.
// Java regex program to check for valid string
import java.util.regex.Matcher;
import java.util.regex.Pattern;
class GFG
{
// Method to check for valid string
static boolean checkString(String str)
{
// regular expression for invalid string
String regex = "[01]{8}";
// compiling regex
Pattern p = Pattern.compile(regex);
// Matcher object
Matcher m = p.matcher(str);
// loop over matcher
while(m.find())
{
// if match found,
// then string is invalid
return false;
}
// if match doesn't found
// then string is valid
return true;
}
// Driver method
public static void main (String[] args)
{
String str = "00000000";
System.out.println(checkString(str) ? "VALID" : "NOT VALID");
}
}
Get Answers For Free
Most questions answered within 1 hours.