Regular Expressions to Validate ISBN Code
Last Updated :
23 Jul, 2025
Given some ISBN Codes, the task is to check if they are valid or not using regular expressions. Rules for the valid codes are:
- It is a unique 10 or 13-digit.
- It may or may not contain a hyphen.
- It should not contain whitespaces and other special characters.
- It does not allow alphabet letters.
Examples:
Input: str = ”978-1-45678-123-4?
Output: True
Input: str = ”ISBN446877428FCI?
Output: False
Explanation: It should contain digits and hyphens only.
Approach: The problem can be solved based on the following idea:
Create a regex pattern to validate the number as written below:
regex = "^(?=(?:[^0-9]*[0-9]){10}(?:(?:[^0-9]*[0-9]){3})?$)[\\d-]+$"
Where,
- ^: Represents the beginning of the string.
- ?: Either it contains or not.
- $: Ending of the string.
- *: Match preceding expression zero or more times
- {n}: Match preceding expression exactly n times
Follow the below steps to implement the idea:
- Create a regex expression for ISBN Codes.
- Use Pattern class to compile the regex formed.
- Use the matcher function to check whether the ISBN Code is valid or not.
- If it is valid, return true. Otherwise, return false.
Below is the implementation of the above approach.
Java
// Java program to validate the
// ISBN Code using Regular Expression
import java.util.regex.*;
class GFG {
// Function to validate the
// ISBN Code
public static boolean isValidISBNCode(String str)
{
// Regex to check valid ISBN Code
String regex
= "^(?=(?:[^0-9]*[0-9]){10}(?:(?:[^0-9]*[0-9]){3})?$)[\\d-]+$";
// Compile the ReGex
Pattern p = Pattern.compile(regex);
// If the str
// is empty return false
if (str == null) {
return false;
}
// Pattern class contains matcher() method
// to find matching between given
// str using regular expression.
Matcher m = p.matcher(str);
// Return if the str
// matched the ReGex
return m.matches();
}
// Driver Code.
public static void main(String args[])
{
// Test Case 1:
String str1 = "978-1-45678-123-4";
System.out.println(isValidISBNCode(str1));
// Test Case 2:
String str2 = "1-56619-909-3";
System.out.println(isValidISBNCode(str2));
// Test Case 3:
String str3 = "1207199818865";
System.out.println(isValidISBNCode(str3));
// Test Case 4:
String str4 = "978-1-12345-909-4 2";
System.out.println(isValidISBNCode(str4));
// Test Case 5:
String str5 = "ISBN446877428FCI";
System.out.println(isValidISBNCode(str5));
}
}
C++
// C++ program to validate the
// ISBN Code using Regular
// Expression
#include <iostream>
#include <regex>
using namespace std;
// Function to validate the
// ISBN Code
bool isValidISBNCode(string str)
{
// Regex to check valid
// ISBN Code.
const regex pattern("^(?=(?:[^0-9]*[0-9]){10}(?:(?:[^0-"
"9]*[0-9]){3})?$)[\\d-]+$");
// If the str
// is empty return false
if (str.empty()) {
return false;
}
// Return true if the str
// matched the ReGex
if (regex_match(str, pattern)) {
return true;
}
else {
return false;
}
}
// Driver Code
int main()
{
// Test Case 1:
string str1 = "978-1-45678-123-4";
cout << isValidISBNCode(str1) << endl;
// Test Case 2:
string str2 = "1-56619-909-3";
cout << isValidISBNCode(str2) << endl;
// Test Case 3:
string str3 = "1207199818865";
cout << isValidISBNCode(str3) << endl;
// Test Case 4:
string str4 = "978-1-12345-909-4 2";
cout << isValidISBNCode(str4) << endl;
// Test Case 5:
string str5 = "ISBN446877428FCI";
cout << isValidISBNCode(str5) << endl;
return 0;
}
Python3
# Python3 program to validate
# ISBN Code using Regular Expression
import re
# Function to validate
# ISBN Code
def isValidISBNCode(str):
# Regex to check valid ISBN Code
regex = "^(?=(?:[^0-9]*[0-9]){10}(?:(?:[^0-9]*[0-9]){3})?$)[\\d-]+$"
# Compile the ReGex
p = re.compile(regex)
# If the string is empty
# return false
if (str == None):
return False
# Return if the string
# matched the ReGex
if(re.search(p, str)):
return True
else:
return False
# Driver code
# Test Case 1:
str1 = "978-1-45678-123-4"
print(isValidISBNCode(str1))
# Test Case 2:
str2 = "1-56619-909-3"
print(isValidISBNCode(str2))
# Test Case 3:
str3 = "1207199818865"
print(isValidISBNCode(str3))
# Test Case 4:
str4 = "978-1-12345-909-4 2"
print(isValidISBNCode(str4))
# Test Case 5:
str5 = "ISBN446877428FCI"
print(isValidISBNCode(str5))
C#
// C# program to validate the
// ISBN Code
// using Regular Expressions
using System;
using System.Text.RegularExpressions;
class GFG {
// Main Method
static void Main(string[] args)
{
// Input strings to Match
// ISBN Code
string[] str
= { "978-1-45678-123-4", "1-56619-909-3",
"1207199818865", "978-1-12345-909-4 2",
"ISBN446877428FCI" };
foreach(string s in str)
{
Console.WriteLine(isValidISBNCode(s) ? "true"
: "false");
}
Console.ReadKey();
}
// method containing the regex
public static bool isValidISBNCode(string str)
{
string strRegex
= @"^(?=(?:[^0-9]*[0-9]){10}(?:(?:[^0-9]*[0-9]){3})?$)[\d-]+$";
Regex re = new Regex(strRegex);
if (re.IsMatch(str))
return (true);
else
return (false);
}
}
JavaScript
// Javascript program to validate
// ISBN Code using Regular Expression
// Function to validate the
// ISBN Code
function isValidISBNCode(str) {
// Regex to check valid
// ISBN CODE
let regex = new RegExp(/^(?=(?:[^0-9]*[0-9]){10}(?:(?:[^0-9]*[0-9]){3})?$)[\d-]+$/);
// if str
// is empty return false
if (str == null) {
return "false";
}
// Return true if the str
// matched the ReGex
if (regex.test(str) == true) {
return "true";
}
else {
return "false";
}
}
// Driver Code
// Test Case 1:
let str1 = "978-1-45678-123-4";
console.log(isValidISBNCode(str1));
// Test Case 2:
let str2 = "1-56619-909-3";
console.log(isValidISBNCode(str2));
// Test Case 3:
let str3 = "1207199818865";
console.log(isValidISBNCode(str3));
// Test Case 4:
let str4 = "978-1-12345-909-4 2";
console.log(isValidISBNCode(str4));
// Test Case 5:
let str5 = "ISBN446877428FCI";
console.log(isValidISBNCode(str5));
Outputtrue
true
true
false
false
Time Complexity: O(N) for each test case, where N is the length of the given string.
Auxiliary Space: O(1)
Related Articles:
Explore
DSA Fundamentals
Data Structures
Algorithms
Advanced
Interview Preparation
Practice Problem