
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Found 7442 Articles for Java

450 Views
ExampleLive Demoimport java.util.regex.Matcher; import java.util.regex.Pattern; public class NoRegTest { public static void main(String[] args) { String s="^fun"; Pattern pattern = Pattern.compile(s); Matcher matcher = pattern.matcher("Java is fun"); if(!matcher.find()) { System.out.println("not found"); } } }Outputnot found

2K+ Views
In this program, we will use a regular expression to check if a given string does not contain the substring "kk" using Java. The regular expression ^((?!kk).)*$ is designed to match strings that do not include the "kk" pattern anywhere in the string. The program will evaluate a sample string and print whether or not it contains "kk". Problem Statement Write a program in Java for checking whether a given string contains the substring "kk." If the string does not contain "kk" the program will return true otherwise it will return false. Input String s = "tutorials" Output true ... Read More

2K+ Views
In this program, we will use a regular expression to check if a given string does not contain the substring "kk" using Java. The regular expression ^((?!kk).)*$ is designed to match strings that do not include the "kk" pattern anywhere in the string. The program will evaluate a sample string and print whether or not it contains "kk". Problem Statement Write a program in Java for checking whether a given string contains the substring "kk." If the string does not contain "kk" the program will return true otherwise it will return false. Input String s = "tutorials" Output true ... Read More

642 Views
Exampleimport java.util.regex.*; class PatternMatch{ public static void main(String args[]) { String content = "I am a student"; String string = ".*boy.*"; boolean isMatch = Pattern.matches(string, content); System.out.println("The line contains 'boy'?"+ isMatch); } }Outputthe line contains 'boy'?falsematches()It is used to check if the whole text matches a pattern. Its output is boolean. It returns true if match is found otherwise false.This is one of simplest and easiest way of searching a String in a text using Regex .There is a another method compile() , if you want ... Read More

1K+ Views
In Java regex matches() matches the input string against the whole string as it add a ^ and $ at the end of the input string.so it will not match the substring. So for matching substring, you should use find().Exampleimport java.util.regex.*; class PatternMatchingExample { public static void main(String args[]) { String content = "aabbcc"; String string = "aa"; Pattern p = Pattern.compile(string); Matcher m = p.matcher(content); System.out.println(" 'aa' Match:"+ m.matches()); System.out.println(" 'aa' Match:"+ m.find()); } }Output'aa' Match:false 'aa' Match:true

815 Views
ExampleLive Demoimport java.util.ArrayList; import java.util.List; import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class SearchRegex { private Pattern subPattern = Pattern.compile(SUBJECT_PATTERN); private Matcher matcher; private static final String SUBJECT_PATTERN = "(?s)Subject 1:\s(.*)Subject 2:"; public static void main(String[] args) { String d = "Subject 1: Java" + "Subject 2: Python"; SearchRegex obj = new SearchRegex(); List list = obj.getSubject(d); System.out.println("Address Result : " + list); } private List getSubject(String d){ List result = new ArrayList(); matcher = subPattern.matcher(d); while (matcher.find()) { result.add(matcher.group(1)); } return result; } }OutputAddress Result : [Java]

389 Views
the syntax? i:x makes the string search case-insensitive. for egpublic class RegCaseSense { public static void main(String[] args) { String stringSearch = "HI we are at java class."; // this won't work because the pattern is in upper-case System.out.println("Try this 1: " + stringSearch.matches(".*CLASS.*")); // the magic (?i:X) syntax makes this search case-insensitive, so it returns true System.out.println("Try this 2: " + stringSearch.matches("(?i:.*CLASS.*)")); } }

389 Views
the syntax? i:x makes the string search case-insensitive. for egpublic class RegCaseSense { public static void main(String[] args) { String stringSearch = "HI we are at java class."; // this won't work because the pattern is in upper-case System.out.println("Try this 1: " + stringSearch.matches(".*CLASS.*")); // the magic (?i:X) syntax makes this search case-insensitive, so it returns true System.out.println("Try this 2: " + stringSearch.matches("(?i:.*CLASS.*)")); } }