Validating Programming File formats using Regular Expressions
Last Updated :
15 Dec, 2022
Given some programming file names, the task is to check if they are valid or not using regular expressions. A valid programming file name follows the below conditions:
- It can contain alphabets (UpperCase or LowerCase), and digits and may contain a hyphen.
- It should contain one dot only that will be used as a suffix to save the file.
- Avoid special characters such as ?, %, #, /, and ‘:‘ in the filename.
- Every file should be saved with the right suffix used for separate programming languages.
- Some of the correct responses of the programming file extension are:
Valid Programming file format:
file_name.extension_of_programming_language
Examples:
Input: file_name.java
Output: True
Input: .PHP_file_name
Output: False
Explanation: The programming file extension is not valid.
Approach: The problem can be solved based on the following idea:
Create a regex pattern to validate the number as written below:
regex= “^.*\.(java|py|c|PHP|cs|cake|cshtml|csx|cpp|cp|cc|cobol|cob|ccp|css|flux|fx| go|jsp|kt|ktm|kts|numpy|numpyw|numsc|pls|pck|pkb|pks|plbplsql|sql|perl|r|rd|rsx|sh|bash|st|cs |swift|vb|class)$“
Where,
- ^ : indicates the start of the file
- .*(dot-star): will match everything except a newline (\n).
- |(Alternation) :Acts like a boolean OR and Matches the expression before or after the |.
- \. (Escape character): matches a dot(.) character.
- *(Quantifier): matches 0 or more of the preceding token(x)Capturing group: Matches x and remembers the match. For example, /(Rahul)/ matches and remembers “Rahul” in “Rahul Chauhan”.
- $ : End of the string
Follow the below steps to implement the idea:
- Create regex expression for the programming file names.
- If the input string is empty, Therefore return false.
- Else, Use the Pattern class to compile the regex formed.
- Use the matcher function to check whether the UPI id is valid or not.
- If it is valid, return true. Otherwise, return false.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
#include <regex>
using namespace std;
string isValidProgrammingFileFormat(string str)
{
const regex pattern(
"^.*\.(java|py|c|PHP|cs|cake|cshtml|csx|cpp|cp|cc|"
"cobol|cob|ccp|css|flux|fx| "
"go|jsp|kt|ktm|kts|numpy|numpyw|numsc|pls|pck|pkb|"
"pks|plbplsql|sql|perl|r|rd|rsx|sh|bash|st|cs "
"|swift|vb|class)$" );
if (str.empty()) {
return "false" ;
}
if (regex_match(str, pattern)) {
return "true" ;
}
else {
return "false" ;
}
}
int main()
{
string str1 = "file_name.java" ;
cout << isValidProgrammingFileFormat(str1) << endl;
string str2 = "file_name01.py" ;
cout << isValidProgrammingFileFormat(str2) << endl;
string str3 = "Java123" ;
cout << isValidProgrammingFileFormat(str3) << endl;
string str4 = "file_name.class" ;
cout << isValidProgrammingFileFormat(str4) << endl;
string str5 = "file_name.css" ;
cout << isValidProgrammingFileFormat(str5) << endl;
string str6 = ".PHP_file_name" ;
cout << isValidProgrammingFileFormat(str6) << endl;
return 0;
}
|
Java
import java.io.*;
import java.util.regex.*;
class GFG {
public static boolean
isValidProgrammingFileFormat(String str)
{
String regex
= "^.*\\.(java|py|c|PHP|cs|cake|cshtml|csx|c|cpp|cp|cc|cobol|cob|ccp|css|flux|fx|go|jsp|kt|ktm|kts|numpy|numpyw|numsc|pls|pck|pkb|pks|plbplsql|sql|perl|r|rd|rsx|sh|bash|st|cs|swift|vb|class)$" ;
Pattern p = Pattern.compile(regex);
if (str == null ) {
return false ;
}
Matcher m = p.matcher(str);
return m.matches();
}
public static void main(String args[])
{
String str1 = "file_name.java" ;
System.out.println(
isValidProgrammingFileFormat(str1));
String str2 = "file_name01.py" ;
System.out.println(
isValidProgrammingFileFormat(str2));
String str3 = "Java123" ;
System.out.println(
isValidProgrammingFileFormat(str3));
String str4 = "file_name.class" ;
System.out.println(
isValidProgrammingFileFormat(str4));
String str5 = "file_name.css" ;
System.out.println(
isValidProgrammingFileFormat(str5));
String str6 = ".PHP_file_name" ;
System.out.println(
isValidProgrammingFileFormat(str6));
}
}
|
Python3
import re
def isValidProgrammingFileFormat( str ):
regex = "^.*\.(java|py|c|PHP|cs|cake|cshtml|csx|cpp|cp|cc|cobol|cob|ccp|css|flux|fx| go|jsp|kt|ktm|kts|numpy|numpyw|numsc|pls|pck|pkb|pks|plbplsql|sql|perl|r|rd|rsx|sh|bash|st|cs |swift|vb|class)$"
p = re. compile (regex)
if ( str = = None ):
return "false"
if (re.search(p, str )):
return "true"
else :
return "false"
if __name__ = = '__main__' :
str1 = "file_name.java"
print (isValidProgrammingFileFormat(str1))
str2 = "file_name01.py"
print (isValidProgrammingFileFormat(str2))
str3 = "Java123"
print (isValidProgrammingFileFormat(str3))
str4 = "file_name.class"
print (isValidProgrammingFileFormat(str4))
str5 = "file_name.css"
print (isValidProgrammingFileFormat(str5))
str6 = ".PHP_file_name"
print (isValidProgrammingFileFormat(str6))
|
C#
using System;
using System.Text.RegularExpressions;
public class GFG
{
public static bool isValidProgrammingFileFormat(String str)
{
var regex = new Regex( "^.*\\.(java|py|c|PHP|cs|cake|cshtml|csx|c|cpp|cp|cc|cobol|cob|ccp|css|flux|fx|go|jsp|kt|ktm|kts|numpy|numpyw|numsc|pls|pck|pkb|pks|plbplsql|sql|perl|r|rd|rsx|sh|bash|st|cs|swift|vb|class)$" );
if (str == null )
{
return false ;
}
var m = regex.Match(str);
return m.Success;
}
public static void Main(String[] args)
{
var str1 = "file_name.java" ;
Console.WriteLine(
isValidProgrammingFileFormat(str1));
var str2 = "file_name01.py" ;
Console.WriteLine(
isValidProgrammingFileFormat(str2));
var str3 = "Java123" ;
Console.WriteLine(
isValidProgrammingFileFormat(str3));
var str4 = "file_name.class" ;
Console.WriteLine(
isValidProgrammingFileFormat(str4));
var str5 = "file_name.css" ;
Console.WriteLine(
isValidProgrammingFileFormat(str5));
var str6 = ".PHP_file_name" ;
Console.WriteLine(
isValidProgrammingFileFormat(str6));
}
}
|
Javascript
function isValidProgrammingFileFormat(str) {
let regex = new RegExp(/^.*\.(java|py|c|PHP|cs|cake|cshtml|csx|cpp|cp|cc|cobol|cob|ccp|css|flux|fx| go|jsp|kt|ktm|kts|numpy|numpyw|numsc|pls|pck|pkb|pks|plbplsql|sql|perl|r|rd|rsx|sh|bash|st|cs |swift|vb|class)$/);
if (str == null ) {
return "false" ;
}
if (regex.test(str) == true ) {
return "true" ;
}
else {
return "false" ;
}
}
let str1 = "file_name.java" ;
console.log(isValidProgrammingFileFormat(str1));
let str2 = "file_name01.py" ;
console.log(isValidProgrammingFileFormat(str2));
let str3 = "Java123" ;
console.log(isValidProgrammingFileFormat(str3));
let str4 = "file_name.class" ;
console.log(isValidProgrammingFileFormat(str4));
let str5 = "file_name.css" ;
console.log(isValidProgrammingFileFormat(str5));
let str6 = ".PHP_file_name" ;
console.log(isValidProgrammingFileFormat(str6));
|
Output
true
true
false
true
true
false
Time Complexity: O(N) for each test case, where N is the length of the given string.
Auxiliary Space: O(1)
Related Articles:
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...