Regex
Using Regex, we can easily match or search for patterns in a text. Before searching for a pattern, we have to specify one using some well-defined syntax.
In this problem, you are given a pattern. You have to check whether the syntax of the given pattern is valid.
Note: In this problem, a regex is only valid if you can compile it using the Pattern.compile method.
import java.util.Scanner;
import java.util.regex.*;
public class Solution
{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
int testCases = Integer.parseInt(in.nextLine());
while(testCases>0){
String pattern = in.nextLine();
try {
Pattern.compile(pattern);
System.out.println("Valid");
} catch(Exception e) {
System.out.println("Invalid");
}
testCases--;
}
in.close();
}
}
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.Scanner;
class Solution{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
while(in.hasNext()){
String IP = in.next();
System.out.println(IP.matches(new MyRegex().pattern));
}
}
}
class MyRegex {
String zeroTo255
= "(\\d{1,2}|(0|1)\\"
+ "d{2}|2[0-4]\\d|25[0-5])";
String pattern = zeroTo255+"\\." +zeroTo255+"\\."+zeroTo255+"\\."+zeroTo255;
}
where:
- \d represents digits in regular expressions, same as [0-9]
- \\d{1, 2} catches any one or two-digit number
- (0|1)\\d{2} catches any three-digit number starting with 0 or 1.
- 2[0-4]\\d catches numbers between 200 and 249.
- 25[0-5] catches numbers between 250 and 255.
In this challenge, we use regular expressions (RegEx) to remove instances of words that are repeated more than once, but retain the first occurrence of any case-insensitive repeated word. For example, the words
love and to are repeated in the sentence I love Love to To tO code. Can you complete the code in the editor so it will turn I love Love to To tO code into I love to code?import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class DuplicateWords {
public static void main(String[] args) {
String regex = "\\b(\\w+)(\\s+\\1\\b)+";
Pattern p = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
Scanner in = new Scanner(System.in);
int numSentences = Integer.parseInt(in.nextLine());
while (numSentences-- > 0) {
String input = in.nextLine();
Matcher m = p.matcher(input);
// Check for subsequences of input that match the compiled pattern
while (m.find()) {
input = input.replaceAll(m.group(), m.group(1));
}
// Prints the modified sentence.
System.out.println(input);
}
in.close();
}
}
According to the policy, a username is considered valid if all the following constraints are satisfied:
- The username consists of to characters inclusive. If the username consists of less than or greater than characters, then it is an invalid username.
- The username can only contain alphanumeric characters and underscores (
_). Alphanumeric characters describe the character set consisting of lowercase characters , uppercase characters , and digits . - The first character of the username must be an alphabetic character, i.e., either lowercase character or uppercase character .
String regularExpression = "^[A-Za-z]\\w{7,29}$";
^: Asserts the start of the string.[A-Za-z]: Matches any uppercase or lowercase alphabetical character.\w: Matches any word character (alphanumeric character plus underscore).{7,29}: Specifies that the preceding\wshould occur between 7 and 29 times.$: Asserts the end of the string.
This regex pattern <(.+?)>([^<]+)</\\1> can be explained as follows:
<: Matches the opening angle bracket<.(.+?): This is a non-greedy capturing group ((.+?)) which matches one or more of any character (.+), but as few as possible (?). The+quantifier means "one or more", and?after it makes it non-greedy.>: Matches the closing angle bracket>.([^<]+): This is another capturing group([^<]+)which matches one or more characters that are not an opening angle bracket ([^<]). This captures the content between the opening and closing tags.</\\1>: This matches the closing tag.</matches the literal characters "</", and\\1is a backreference to the content captured by the first capturing group, ensuring that the closing tag matches the opening tag.
Overall, this regex pattern is used to match pairs of XML-like tags and capture their content.
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
int testCases = Integer.parseInt(in.nextLine());
while(testCases>0){
String line = in.nextLine();
System.out.println(tagContentExtractor(line));
testCases--;
}
in.close();
}
private static String tagContentExtractor(String line) {
Pattern pattern = Pattern.compile("<(.+?)>([^<]+)</\\1>");
Matcher matcher = pattern.matcher(line);
StringBuilder result = new StringBuilder();
while (matcher.find()) {
result.append(matcher.group(2)).append('\n');
}
return result.length() > 0 ? result.toString().trim() : "None";
}
}
Comments
Post a Comment