Given a string of 0 and 1, we need to check that the given string is valid or not. The given string is valid when there is no zero is present in between 1’s. For example, 1111, 0000111110, 1111000 are valid strings but 01010011, 01010, 101 are not.
Input : 100 Output : VALID Input : 1110001 Output : NOT VALID There is 0 between 1s Input : 00011 Output : VALID
As we know that in a string if there is zero between 1’s, than string is not valid.Hence below is one of the regular expression for invalid string pattern.
So here is the simple regex algorithm.
- Loop over the matcher(string)
- if above regex match is find in the matcher, then string is not valid, otherwise valid.
This article is contributed by Gaurav Miglani. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Check if a given string is a valid number (Integer or Floating Point) in Java | SET 2 (Regular Expression approach)
- Check if a binary string has a 0 between 1s or not | Set 1 (General approach)
- Find all the patterns of "1(0+)1" in a given string | SET 2(Regular Expression Approach)
- How to check string is alphanumeric or not using Regular Expression
- Check if an URL is valid or not using Regular Expression
- How to check Aadhar number is valid or not using Regular Expression
- std::regex_match, std::regex_replace() | Regex (Regular Expression) In C++
- Remove duplicate words from Sentence using Regular Expression
- How to validate identifier using Regular Expression in Java
- How to validate time in 12-hour format using Regular Expression
- Validating Roman Numerals Using Regular expression
- How to validate time in 24-hour format using Regular Expression
- How to validate pin code of India using Regular Expression
- How to validate Visa Card number using Regular Expression
- How to validate Indian Passport number using Regular Expression
- How to validate PAN Card number using Regular Expression
- How to validate Hexadecimal Color Code using Regular Expression
- Finding Data Type of User Input using Regular Expression in Java
- How to validate GST (Goods and Services Tax) number using Regular Expression
- How to validate HTML tag using Regular Expression