0% found this document useful (0 votes)
5 views21 pages

Java Programs Interview Questions

Uploaded by

darlingsayed1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views21 pages

Java Programs Interview Questions

Uploaded by

darlingsayed1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 21

Numbers:

Q. How do you swap two numbers without using a third variable in Java?
public static void main(String[] args) {
int a = 10;
int b = 20;
[Link]("a is " + a + " and b is " + b);

a = a + b;
b = a - b;
a = a - b;

[Link]("After swapping, a is " + a + " and b is " + b);


}

Q. Write a Java program to check if the given number is a prime number.

public static void main(String[] args) {


int low = 100, high = 200;
List<Integer> list=new ArrayList<>();

for(int i=low;i<=high;i++)
{
if(isprime(i))
[Link](i);
}
}
public static boolean isprime(int num)
{
if(num==0 || num==1)
{
return false;
}
for(int i=2;i<=num/2;i++)
{
if(num%i==0)
{
return false;
}
}
return true;
}

With another logic


import [Link].*;

public class Main {


public static void main(String[] args) {
int a=50,b=100;

int[] arr= new int[b-a];


for(int i=a;i<b;i++)
{
arr[i-a] = i;
}
for(int i=0;i<[Link];i++)
{
if(arr[i]==0)
{
continue;
}
else{
int temp= arr[i];
if(myfunction(arr[i]))
{
[Link](temp);

}
else
{
arr[i]=0;
}

for(int j=i+temp;j<[Link];j=j+temp)
{
arr[j]=0;

}
}
}

}
public static boolean myfunction(int a)
{
for(int i=2;i<a/2;i++)
{
if(a%i==0)
{
return false;
}
}
return true;
}

[Link] HYPERLINK
"[Link]
interview-questions" a Java program to print a Fibonacci sequence using
recursion.
A Fibonacci sequence is one in which each number is the sum of the two previous numbers

public static void printFibonacciSequence(int count) {


int a = 0;
int b = 1;
int c = 1;
int num = 8;

for(int i=1;i<=num;i++)
{
[Link](a+" ");
a=b;
b=c;
c=a+b;
}
}
OR
public static void printFibonacciSequence(int count) {
int a = 0;
int b = 1;
int c = 1;

for (int i = 1; i <= count; i++) {


[Link](a + ", ");

a = b;
b = c;
c = a + b;
}
}

public static void main(String[] args) {


printFibonacciSequence(10);
}

[Link] HYPERLINK
"[Link]
interview-questions" do you check if a list of integers contains only odd
numbers in Java?

public static void main(String[] args) {


int[] list = {1,4,6,8};
boolean flag=true;
for(int i : list) {
if(i%2 == 0) {
flag =false;
}
}
if(flag) [Link]("List contains only Odd values");
else [Link]("List contains Even values");
}

[Link] HYPERLINK
"[Link]
interview-questions" can you find the factorial of an integer in Java?

public static void main(String[] args) throws ParserConfigurationException,


SAXException, IOException
{
long fact=1;
int number=4;//It is the number to calculate factorial
fact = factorial(number);
[Link]("Factorial of "+number+" is: "+fact);
}
public static long factorial(long n) {
if (n == 1)
return 1;
else
return (n * factorial(n - 1));
}

Arrays:

Arrays Class methods:


[Link](array); - returns List of objects
[Link](int[],char[],Objects[])
[Link](char[], int[] , Object[])

Q. Write Java program that checks if two arrays contain the same elements.

public static void main(String[] args) {

Integer[] a1 = {1,2,3,2,1};
Integer[] a2 = {1,2,3};
Integer[] a3 = {1,2,3,4};

[Link](sameElements(a1, a2));
[Link](sameElements(a1, a3));
}

static boolean sameElements(Object[] array1, Object[] array2)


{
Set<Object> uniqueElements1 = new HashSet<>([Link](array1));
Set<Object> uniqueElements2 = new HashSet<>([Link](array2));

// if size is different, means there will be a mismatch


if ([Link]() != [Link]()) return false;

for (Object obj : uniqueElements1)


{
// element not present in both?
if (![Link](obj)) return false;
}
return true;
}

[Link] HYPERLINK
"[Link]
interview-questions" do you get the sum of all elements in an integer array
in Java?

public static void main(String[] args) {


int[] arr= { 1, 2, 3, 4, 5 };
int sum = 0;

for(int i : arr)
{
sum=sum+i;
}
[Link](sum);
}

[Link] HYPERLINK
"[Link]
interview-questions" do you find the second largest number in an array in
Java?

public static void main(String[] args) {


int[] arr= { 3, 5, 8, 4, 3 };
int highest = Integer.MIN_VALUE;
int SecondHighest = Integer.MIN_VALUE;

for(int i : arr)
{
if(i>highest) {
SecondHighest = highest;
highest = i;
}
else if (i>SecondHighest) SecondHighest = i;
}
}

Q. program to sort array in ascending order ?


public static void main(String[] args) {
// Create a Scanner object to read input from the console
int arr[]={5,2,9,1,6};
int temp = 0;
for (int i=0;i<[Link];i++)
{
for(int j=i+1;j<[Link];j++)
{
if(arr[i]>arr[j])
{
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
}
for(int i=0;i<[Link];i++)
{
[Link](arr[i]);
}
}

[Link] HYPERLINK
"[Link]
interview-questions" do you sort an array in Java?

public static void main(String[] args)


{
int[] array = {1, 2, 3, -1, -2, 4};
[Link](array);
[Link]([Link](array));
}
Example for Descending order:

for(int i=[Link]-1;i<[Link];i--)
{
if(i<0) return;
[Link](arr[i]);
}

Strings:
StringBuffer Methods: append(str)
insert(1,char or str)
delete(start,end)
deleteCharAt(index)
length()
reverse()
toString()

String methods:
charAt() - Returns the character at the specified index (position)

public static void main(String[] args) {


Integer intobj = 5;
Float floatobj=3.14f;
Long longobj= (long) 600000;

[Link](); [Link]();
[Link]();
[Link](); [Link](); [Link]();
[Link](); [Link](); [Link]();

[Link] to print without main method “hello”?


public class SchoolMember
{
static {
[Link](“Hello World”);
[Link](0);
}
}

Q. How do you check whether a string is a palindrome in Java?


-> A palindrome string is the same string backwards or forwards
public static void main(String[] args) {

public static void main(String[] args) {


String str="Liril";
String rev="";
for(int i =[Link]()-1;i>=0;i--) {
rev= rev+[Link](i);
}
if([Link](rev)) [Link]("Given String is
Palindrome");
else [Link]("Given string is not a Palindrome");
}

[Link] a program to print your name in Java?

public static void main(String[] args)


{
Scanner input=new Scanner([Link]);
String fname=[Link]();
String lname=[Link]();
[Link](fname+lname);
}

[Link] only the non duplicate words in the string?


public static void main(String[] args){
String str = "Big black bug bit a big black dog on his big black
nose";
int count;

str=[Link]();
String words[] = [Link](" ");

for(int i=0;i<[Link];i++)
{
count =1;
for(int j=i+1;j<[Link];j++)
{
if(words[i].equals(words[j]))
{
count++;
[Link](words[j]);
// Set words[j] to 0 to avoid printing visited word
words[j]="0";
}
}
if(count>1 && words[i]!="0")
{
[Link](words[i]);
}
}
}

Q. Remove the only duplicate words from the String?


public static void main(String[] args) {
[Link]("");
String str="ali is using is java using program";
String[] arr=[Link]("\\s");
String str1="";
Set<String> set=new LinkedHashSet<String>([Link](arr));
[Link](set);
for(String s: set)
{
str1=str1+" "+s;
}
[Link](str1);
}

[Link] to find out the length of the string without using length function

public static void main(String[] args)


{
String str="Ali Sayed";
int count = 0;
for(char c : [Link]())
{
count++;
}
[Link]("Length of the String is :"+count);
}

Q java program to reverse String ?


public static void main(String[] args)
{
String str= "Geeks", nstr="";
char ch;
[Link]("Geeks"); //Example word

for (int i=0; i<[Link](); i++)


{
ch= [Link](i); //extracts each character
nstr= ch+nstr; //adds each character in front of the existing
string
}
[Link]("Reversed word: "+ nstr);
}
OR

public static void main(String[] args) {


String strInitial = "SAYED";

StringBuffer sb =new StringBuffer(strInitial);


[Link]();
[Link](sb);
}

[Link] number of occurrences of chars in a String

public static void main(String args[])


{
String someString = "sayed ali";
char temp[] = [Link]();

for (int i = 0; i < [Link](); i++) {


int count = 0;
for(int j=0;j<[Link]();j++) {
if ([Link](i) == temp[j]) {
count++;
}
}
[Link]("number of occurances of
"+[Link](i)+"
is"+count);
}

}
OR
public static void main(String a[])
{
String st= "WelcomeztozInfosys";
char[] arr = [Link]().toCharArray();

Map<Character,Integer> map=new HashMap<Character,Integer>();


for(Character c: arr)
{
if([Link](c))
{
[Link](c,[Link](c)+1);
}
else [Link](c,1);
}
for([Link] entry : [Link]())
{
[Link]([Link]()+" "+[Link]());
}
}

[Link] the only few Characters from a given String ?


public static void main(String[] args) {
String str="Ali Sayed";
StringBuffer sb=new StringBuffer([Link](0,2));
[Link]();
[Link](sb);
[Link]([Link](2,[Link]()));
[Link](sb);
}

OR

public class Main {


public static void main(String[] args) {
String str = "Sayed Ali";
String toreplace="ye";
int StartIndex=[Link]("ye");

if(StartIndex!=-1)
{
int endIndex=StartIndex+[Link]();

StringBuffer sb=new
StringBuffer([Link](StartIndex,endIndex));
[Link]();

String result = [Link](0,StartIndex)+[Link]()


+[Link](endIndex,[Link]());
[Link](result);
}
}
}

Q. Program to Get the number of letters , alphabets and others in you gmail
account?
public static void main(String[] args) throws Exception {

String gmail ="alibuddu578@[Link]";

char[] arr=[Link]();
[Link]([Link]());

int letters=0, digits=0,space=0,others=0;


for (char c: arr)
{
if([Link](c))
{
letters++;
}
else if([Link](c))
{
digits++;
}
else if([Link](c))
{
space++;
}
else others++;
}

[Link]("Countof the Letters is :"+letters+"\nCount of


digits is :"+digits +"\nCount if the Spaces is :"+space+"\nCount of
Special charactersis :"+others);
}

Regular Expression for password:


^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%^&+=])(?=\\S+$).{8,}$
^ # start-of-string
(?=.*[0-9]) # a digit must occur at least once
(?=.*[a-z]) # a lower case letter must occur at least once
(?=.*[A-Z]) # an upper case letter must occur at least once
(?=.*[@#$%^&+=]) # a special character must occur at least once
(?=\\S+$) # no whitespace allowed in the entire string
.{8,} # anything, at least eight places though
$ # end-of-string

public static void main(String[] args) {


String password="Alisayed@578";
String regexp="^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%^&+=])(?
=\\S+$).{8,20}$";

if([Link](regexp)) [Link]("Success");
else [Link]("Failed");
}
----OR-----
public static void main(String[] args) {
[Link]("Hello, World!");

String password="Alisayed@578";
String regexp="^(?=.*[0-9])(?=.*[A-Z])(?=.*[a-z])(?=.*[@#$%^&+=])(?
=\\S+$).{8,12}$";
Pattern pattern=[Link](regexp,Pattern.CASE_INSENSITIVE);
Matcher matcher=[Link](password);
if([Link]()) [Link]("Success");
else [Link]("Failed");
}

Q. Write a Java program to check if a vowel is present in a string.

public static void main(String[] args) {


String password="Alisayed@578";
String regexp=".*[aeiou].*";

if([Link](regexp)) [Link]("Success");
else [Link]("Failed");
}

[Link] HYPERLINK "[Link]


programming-interview-questions" do you remove spaces from a string in Java?

public static void main(String[] args) {


String str="Liril ekkuva indra", temp="";

str=[Link]("\\s", "");
[Link]("After Removing thespaces: "+str);

/*to remove special chars, [Link](“[^a-zA-Z0-9]”)

}
Q. Program to get only characters or digits or special characters from a String?

public static void main(String[] args) {


[Link]("Hello, World!");
String str="ali_buddu-578@gma il#com";

String letters=[Link]("[^a-zA-Z]","");
[Link](letters+" size is "+[Link]());

String digits=[Link]("[^0-9]","");
[Link](digits+" size is "+[Link]());

String space=[Link]("[^\\s]","");
[Link](space+" size is "+[Link]());

String specialChars=[Link]("[^@#$%&*_-]","");
[Link](specialChars+" size is "+[Link]());
[Link] HYPERLINK "[Link]
programming-interview-questions" do you remove leading and trailing spaces
from a string in Java?

public static void main(String[] args) {


String s = " abc def\t";
s = [Link]();
[Link](s);
}

[Link] HYPERLINK "[Link]


programming-interview-questions" do you remove all occurrences of a given
character from an input string in Java?

public static void main(String[] args) {


String str1 = "abcd1ABCDab5cdABCD";

str1 = [Link]("a", "");

[Link](str1); // bcdABCDbcdABCD
}

[Link] HYPERLINK "[Link]


programming-interview-questions" can you find a string in a text file in Java?
public static void main(String[] args) {
File =new File("Path");
Scanner scan = new Scanner(file);

while([Link]())
{
String line = [Link]();
if([Link](str))
{
[Link]();
return true
}
}
[Link]();
return false;
}

Collections:
Q. What datatype can be added to a List?

Integer or String in angle brackets (<>)

8. Collection: Collection is an interface which will provides the standard


functionality of data structure to List , Set, Map and Queue. Collection is a set of Objects.

Collections : Collections is a class that provides the static methods which can be used
for varies operations on a Collection.
Collection Methods:
[Link]() [Link](array,[Link]());
[Link](List)
[Link](List)
[Link](List,from,To);
Collections Class will be used to sort and synchronize the collection elements.
Insertion Random Null
DataStructure extends impliments Duplicates Order Access Synchronized Values
Abstract List
ArrayList Class List Allow Yes Yes No Yes
Abstract List
LinkedList Class List Allow Yes Yes No Yes
One Null
HashSet AbstractSet Set No No No No Value
One Null
LinkedHashSet HashSet Set No Yes No No Value
Not Keys, Only one Null
HashMap AbstractMap Map values No No No Key
Not Keys, Only one Null
LinkedHashMap HashMap Map values Yes No No Key

ArrayList:
Its inherited from Abstract List class and implements List Interface.
Follows Insertion Order
Its allows us to access the list randomly
Size of the list will be increased as the collection grows
It is not Synchronized.

List<String> interceptScreenScans = new ArrayList<String>();

[Link]("IPA");
[Link]("STI");
[Link]("STO");
intercept [Link]("DEP");
if ([Link] (scanMenu))

Methods:
Add(str) : Appends the specified element to the end of this list
Add(index,str) : Inserts the specified element at the specified position in this list
Clear() : Removes all of the elements from this list (optional operation).
The list will be empty after this call returns.
Contains(Object) : Returns true if this list contains the specified element
Get(index) : Returns the element at the specified position in this list.
indexOf(Object) : Returns the index of the first occurrence of the specified element
in this list Returns the index of the first occurrence of the specified element in this list
iterator() : Returns an iterator over the elements in this list in proper
sequence.
equals(Object 0) : Compares the specified object with this list for equality.
Remove(index) : Removes the element at the specified position in this list
Remove(Object) :Removes the first occurrence of the specified element from this
list
removeAll(list) : Removes from this list all of its elements that are contained in the
specified collection
set(index,str) : Replaces the element at the specified position in this list with the
specified element
size() : Returns the number of elements in this list.
subList(frmIndex,ToIndex) : Returns a view of the portion of this list between
the specified fromIndex, inclusive, and toIndex.

HashMap:
Hashmap is implemented form the Map interface
Hashmap does not allows duplicate Keys but allows duplicate values
Hashmap allows one null key but it allows many null values
Hashmap is not an ordered collection

Methods:
Clear():Removes all of the mappings from this map
ContainsValue(object)
entrySet(): Returns set of Keys
equals(Object): Compares the specified object with this map for equality. Returns true
if the given object is also a map and the two maps represent the same mappings.
Get(Object Key): Returns the value to which the specified key is mapped
keyset():Returns a Set view of the keys contained in this map
put(key,value): Associates the specified value with the specified key in this map
remove(key)
remove(key,value):
replace(key,value):
size();
toString();Returns a string representation of the object.

private String getStagingAreaListBoxDesc (String location)


Map<String, String> desc = new HashMap<String, String>();
[Link]("MEM", "FX: FEDEX");
[Link] ("YYZ", "FX: FEDEX");
[Link]("EWR", "FX: FEDEX");
[Link]("ŸMX", "FX: FEDEX") ;
[Link] ("HNL", "FX: FEDEX");
return [Link]([Link]());

Set Examples:
public static void main(String a[])
{
WebDriver driver = new ChromeDriver();
Set<String> ids= [Link]();
Iterator<String> itr =[Link]();
String parentID=[Link]();
String chidID = [Link]();
[Link]().window(chidID);
[Link]([Link]());
[Link]().window(parentID);
}

[Link] a program to add elements to a list?

public class MyClass


{
public static void main(String[] args)
{
List<String> listNew = new ArrayList<String>();
[Link]("Ali");
[Link]("sayed");
}
}

[Link] a program to iterate through a List?


public static void main(String a[])
{
List<String> city = [Link]("Boston", "San Diego", "Las Vegas",
"Houston", "Miami", "Austin");
Iterator<String> cityIterator = [Link]();
while([Link]())
{
[Link]([Link]());
}
}

Q.A particular list of buttons are there?Write the xpath for it?
[Link]([Link]("String"));

Q. How do you reverse a linked list in Java?

public static void main(String[] args) {

LinkedList<Integer> l1 = new LinkedList<>();

[Link](1);
[Link](2);
[Link](3);
LinkedList<Integer> rev= new LinkedList<Integer>();
for(int i=[Link]()-1; i>=0;i--)
{
[Link]([Link](i));
}

[Link]("Linked List before Reversing : "+l1);


[Link]("Linkedist after Reversing : "+rev);

}
[Link] HYPERLINK
"[Link]
interview-questions" do you merge two lists in Java?

public static void main(String[] args) {


List<String> list1 =new ArrayList<String>();
[Link]("A1");
List<String> list2 = new ArrayList<String>();
[Link]("A2");

[Link](list2);
[Link](list1);
}
Q. Converting Set to List without changing the order?

public static void main(String a[])


{
Set<String> s = new HashSet<String>();
[Link]("Geeks");
[Link]("for");

int n = [Link]();
List<String> aList = new ArrayList<String>(n);
for (String x : s)
[Link](x);

[Link]("Created ArrayList is");


for (String x : aList)
[Link](x);
}

[Link] HYPERLINK
"[Link]
interview-questions" a Java program that sorts HashMap by value.

public static void main(String[] args) {


Map<String, Integer> scores = new HashMap<>();

[Link]("David", 95);
[Link]("Jane", 80);
[Link]("Mary", 97);
[Link]("Lisa", 78);
[Link]("Dino", 65);

[Link](scores);
scores = sortByValue(scores);
[Link](scores);
}

private static Map<String, Integer> sortByValue(Map<String, Integer>


scores) {
Map<String, Integer> sortedByValue = new LinkedHashMap<>();
// get the entry set
Set<[Link]<String, Integer>> entrySet = [Link]();
[Link](entrySet);

// create a list since the set is unordered


List<[Link]<String, Integer>> entryList = new
ArrayList<>(entrySet);
[Link](entryList);

// sort the list by value


[Link]((x, y) -> [Link]().compareTo([Link]()));
[Link](entryList);

// populate the new hash map


for ([Link]<String, Integer> e : entryList)
{
[Link]([Link](), [Link]());
[Link]([Link]()+" - "+[Link]());
}
return sortedByValue;
}

[Link] HYPERLINK
"[Link]
interview-questions" do you compile and run a Java class from the
command line?

public class Test {

public static void main(String args[]) {


[Link]("Hi");
}
}

$ javac [Link]
$ java Test

[Link] to read the values from the xml file?


public static String getValue(String filepath, String tagname) throws
ParserConfigurationException, SAXException, IOException
{
String path=
[Link]("[Link]").toString()+"/resouses/xml/"+"GlobalEnv
.xml";

File file=new File(filepath);


DocumentBuilderFactory dbf = [Link]();
DocumentBuilder builder = [Link]();
Document document= [Link](file);
return
[Link]("username").item(0).getTextContent().trim();
}
Q. how to connect to the Database using java?
public static void main(String a[])
{
try{
String dbURL =
"jdbc:oracle:thin:tiger/scott@localhost:1521:productDB";
Connection con = [Link](dbURL);
Statement stmt=[Link]();
ResultSet rs=[Link]("select * from emp");

// Closing the connections


[Link]();
}

// Catch block to handle exceptions


catch (Exception ex) {
// Display message when exceptions occurs
[Link](ex);
}
}

Q. How to call objects from properties file


FileOutputStream fos = new FileOutputStream(“resources/[Link]”);
Properties prop=new Properties();
[Link](“uname”,”alisayed”);
[Link](fos,null);
FileInputStream fis=new FileInputStream(“resources/[Link]”);
[Link](fis);
[Link](“username”);
public static void main(String[] args) {
try {
FileOutputStream fos =new
FileOutputStream("resources//[Link]");
Properties prop = new Properties();

[Link]("[Link]", "localhost");
[Link]("[Link]", "mkyong");
[Link]("[Link]", "password");

[Link](fos, null);

FileInputStream fis= new


FileInputStream("resources//[Link]");
[Link](fis);

[Link]([Link]("[Link]"));

} catch (IOException e) {
// TODO Auto-generated catch block
[Link]();
}

}
Q. program for reading a excel file
Shortcuts:
FileInputStream(path)
XSSFWorkbook wb = new XSSFWorkbook(fis);
XSSFSheet sheet = [Link](0);
Row =[Link](1);
Cell = [Link](1);

Row row3 = [Link](3);


Cell cell2 = [Link](2);
[Link](“Ali”);
FileOutputStream fos =new FileOutputStream(path);
[Link](fos);
[Link]();

public static void main(String] args)


{
FileInputStream fis = new FileInputStream(“C:\\Users\\SayedAli\\
Desktop\\[Link]”);

XSSFWorkbook workbook = new XSSFWorkbook(fis);


XSSFSheet sheet = [Link](0);
Row = [Link](0);
Cell = [Link](1);

[Link](cell);
[Link]([Link](3));
[Link]([Link]());

//To write inExcel we will use below code


Row row=[Link](5);
Cell cell= [Link](0);
[Link]("alisayed");

FileOutputStream fos=new FileOutputStream("C:\\Users\\Sayed


Ali\\Desktop\\[Link]");
[Link](fos);
[Link]();

Q. Example using format and Calendar classes


import [Link].*;
import [Link].*;
import [Link].*;

public class Main {


public static void main(String[] args) {
[Link]("Hello, World!");

Calendar cal=[Link]();
SimpleDateFormat sdf=new SimpleDateFormat("MM/dd/yyyy");
//Incrementing the Date
String currentDate=[Link]([Link]());

//Decremeting the Date


[Link](Calendar.DAY_OF_MONTH,-2);
String desiredDate = [Link]([Link]());

//Incremnting the Month


[Link]([Link],3);
String updatedMonth=[Link]([Link]());

//Incremnting the year


[Link]([Link],3);
String updatedYear=[Link]([Link]());

[Link]("Present Date is : "+ currentDate);


[Link]("Present Date is : "+ desiredDate);
[Link]("Present Date is : "+ updatedMonth);
[Link]("Present Date is : "+ updatedYear);

}
}

Regular Expressions:
Meta characters
. : Matches any single character, except newline characters.
^ : Asserts the start of a line.
$ : Asserts the end of a line.
| : Acts as a logical OR operator.
Character Classes
[abc] : Matches any one of the characters a, b, or c.
[^abc] : Negates the set; matches any character except a, b, or c.
[a-zA-Z]: Specifies a range, matching any letter from a to z or A to Z.
Predefined Character Classes
\\d : Matches any digit, equivalent to [0-9].
\\D : Matches any non-digit.
\\s : Matches any whitespace character.
\\S : Matches any non-whitespace character.
\\w : Matches any word character (alphanumeric & underscore).
\\W : Matches any non-word character.
Quantifiers
`` :Zero or more times.
+: :One or more times.
?: :Zero or one time; also used to denote a non-greedy quantifier.
{n}: :Exactly n times.
{n,} :n or more times.
{n,m} :Between n and m times, inclusive.
Special Constructs
(abc) : A capturing group that matches the sequence abc.
(?:abc): A non-capturing group.
(?i)abc: An inline flag for case-insensitive matching of abc.
\\\\b : A word boundary.
\\\\B : A non-word boundary.
Backreferences
\\\\1, \\\\2, ... : Matches the same text as previously matched by a capturing
group.
Boundary Matchers
^ : Matches the beginning of a line.
$ : Matches the end of a line.
\\\\b : Matches a word boundary.
\\\\B : Matches a non-word boundary.
Flags
(?i) : Enables case-insensitive matching.
(?s) : Enables dot-all mode, where . matches any character, including newline
characters.
Anchors
\\\\A : Matches the beginning of the input.
\\\\Z : Matches the end of the input, or before a newline at the end.
\\\\z : Matches the end of the input.
Logical Operators
(?=...) : Positive lookahead assertion.
(?!...) : Negative lookahead assertion.
(?<=...): Positive lookbehind assertion.
(?<!...) : Negative lookbehind assertion.

You might also like