aster.cloud aster.cloud
  • /
  • Platforms
    • Public Cloud
    • On-Premise
    • Hybrid Cloud
    • Data
  • Architecture
    • Design
    • Solutions
    • Enterprise
  • Engineering
    • Automation
    • Software Engineering
    • Project Management
    • DevOps
  • Programming
    • Learning
  • Tools
  • About
  • /
  • Platforms
    • Public Cloud
    • On-Premise
    • Hybrid Cloud
    • Data
  • Architecture
    • Design
    • Solutions
    • Enterprise
  • Engineering
    • Automation
    • Software Engineering
    • Project Management
    • DevOps
  • Programming
    • Learning
  • Tools
  • About
aster.cloud aster.cloud
  • /
  • Platforms
    • Public Cloud
    • On-Premise
    • Hybrid Cloud
    • Data
  • Architecture
    • Design
    • Solutions
    • Enterprise
  • Engineering
    • Automation
    • Software Engineering
    • Project Management
    • DevOps
  • Programming
    • Learning
  • Tools
  • About
Keyboard | Regex
  • Engineering
  • Software Engineering

The Prime Suspect. Unraveling The Regex Riddle.

  • Dean Marc
  • June 25, 2023
  • 2 minute read

The regex pattern provided is interesting as it checks whether a number is prime or not, but it does so in a very unusual way. This pattern works only for unary/binary numbers. It’s not checking for divisibility or factors in the traditional sense. Instead, it’s using the properties of regular expressions and the way they match patterns.

^1?$: This part of the expression checks if the string is either empty or contains a single ‘1’. This is because the ? makes the preceding character (in this case ‘1’) optional. The ^ and $ are start and end of string anchors, respectively. This part of the expression will match if the input string is ‘1’ or an empty string.


Partner with aster.cloud
for your next big idea.
Let us know here.



From our partners:

CITI.IO :: Business. Institutions. Society. Global Political Economy.
CYBERPOGO.COM :: For the Arts, Sciences, and Technology.
DADAHACKS.COM :: Parenting For The Rest Of Us.
ZEDISTA.COM :: Entertainment. Sports. Culture. Escape.
TAKUMAKU.COM :: For The Hearth And Home.
ASTER.CLOUD :: From The Cloud And Beyond.
LIWAIWAI.COM :: Intelligence, Inside and Outside.
GLOBALCLOUDPLATFORMS.COM :: For The World's Computing Needs.
FIREGULAMAN.COM :: For The Fire In The Belly Of The Coder.
ASTERCASTER.COM :: Supra Astra. Beyond The Stars.
BARTDAY.COM :: Prosperity For Everyone.

^(11+?)\1+$: This part of the expression is a bit more complex. It’s looking for a pattern of one or more ‘1’s (11+?) at the start of the string (^) that repeats one or more times throughout the rest of the string (\1+$). The +? is a non-greedy quantifier, meaning it will try to match the smallest possible string that satisfies the condition. The \1 is a backreference to the first captured group, which is the (11+?) part of the expression.

So, if the string of ‘1’s can be evenly divided into smaller identical strings of ‘1’s, then it’s not a prime number. If it can’t be divided in this way, then it is a prime number.

For example, if the input string is ‘1111’ (representing the number 4), the (11+?) part of the expression will match ’11’, and the \1+$ part of the expression will match the remaining ’11’, so the whole expression will match, indicating that 4 is not a prime number.

Read More  Even More Pi In The Sky: Calculating 100 Trillion Digits Of Pi On Google Cloud

If the input string is ‘111’ (representing the number 3), the (11+?) part of the expression will match ‘1’, but the \1+$ part of the expression won’t be able to match the remaining ’11’ with a single ‘1’, so the whole expression won’t match, indicating that 3 is a prime number.

This regular expression is a neat trick, but it’s not a practical way to check for prime numbers in most situations. It’s more of a demonstration of the power and flexibility of regular expressions.


For enquiries, product placements, sponsorships, and collaborations, connect with us at [email protected]. We'd love to hear from you!

Our humans need coffee too! Your support is highly appreciated, thank you!

Dean Marc

Part of the more nomadic tribe of humanity, Dean believes a boat anchored ashore, while safe, is a tragedy, as this denies the boat its purpose. Dean normally works as a strategist, advisor, operator, mentor, coder, and janitor for several technology companies, open-source communities, and startups. Otherwise, he's on a hunt for some good bean or leaf to enjoy a good read on some newly (re)discovered city or walking roads less taken with his little one.

Related Topics
  • Programming
  • Regex
You May Also Like
Web
View Post
  • Engineering
  • Software Engineering

Mastering the Art of Load Testing for Web Applications

  • November 29, 2023
Ubuntu. Chiselled containers.
View Post
  • Engineering
  • Technology

Canonical Announces The General Availability Of Chiselled Ubuntu Containers

  • November 25, 2023
Brush, Color, and Sketch pad
View Post
  • Cloud-Native
  • Design
  • Engineering

6 Security Best Practices For Cloud-Native Applications

  • November 17, 2023
Ingrasys
View Post
  • Computing
  • Engineering
  • Technology

Ingrasys Unveils Next-Gen AI And Cooling Solutions At Supercomputing 2023

  • November 15, 2023
Malware, Security, and Laptop
View Post
  • Engineering
  • Technology

Singapore And Google Partner On Web Risk To Protect Citizens From Online Scams And Phishing

  • November 12, 2023
View Post
  • Engineering
  • Public Cloud

Golang’s GORM Support For Cloud Spanner Is Now Generally Available

  • November 9, 2023
Cloud
View Post
  • Design
  • Engineering
  • Public Cloud

The Impact Of Public Cloud Price Hikes

  • November 8, 2023
View Post
  • Engineering

Finding Stability in Open Source Work

  • November 2, 2023

Stay Connected!
LATEST
  • Web 1
    Mastering the Art of Load Testing for Web Applications
    • November 29, 2023
  • Data center. Servers. 2
    Intel Granulate Optimizes Databricks Data Management Operations
    • November 27, 2023
  • Ubuntu. Chiselled containers. 3
    Canonical Announces The General Availability Of Chiselled Ubuntu Containers
    • November 25, 2023
  • Cyber Monday Sale. Guzz. Ideals collection. 4
    Decode Workweek Style with guzz
    • November 23, 2023
  • Guzz. Black Friday Specials. 5
    Art Meets Algorithm In Our Exclusive Shirt Collection!
    • November 23, 2023
  • Presents. Gifts. 6
    25 Besties Bargain Bags Below $100 This Black Friday 2023
    • November 22, 2023
  • Electronics 7
    Top 10+1 You Can’t Do Without For The Holidays: Electronics Edition.
    • November 20, 2023
  • Microsoft. Windows 8
    Ousted Sam Altman To Lead New Microsoft AI Team
    • November 20, 2023
  • Sale. Deals. Discount. 9
    The 50 Best Electronic Deals To Get On Amazon Before Cyber Monday 2023
    • November 20, 2023
  • Portrait of Rosalynn Carter, 1993 10
    Former First Lady Rosalynn Carter Passes Away at Age 96
    • November 19, 2023
about
Hello World!

We are aster.cloud. We’re created by programmers for programmers.

Our site aims to provide guides, programming tips, reviews, and interesting materials for tech people and those who want to learn in general.

We would like to hear from you.

If you have any feedback, enquiries, or sponsorship request, kindly reach out to us at:

[email protected]
Most Popular
  • Oracle | Microsoft 1
    Oracle Cloud Infrastructure Utilized by Microsoft for Bing Conversational Search
    • November 7, 2023
  • Riyadh Air and IBM 2
    Riyadh Air And IBM Sign Collaboration Agreement To Establish Technology Foundation Of The Digitally Led Airline
    • November 6, 2023
  • Ingrasys 3
    Ingrasys Unveils Next-Gen AI And Cooling Solutions At Supercomputing 2023
    • November 15, 2023
  • Cloud 4
    DigitalOcean Currents Report Finds That Adoption Of AI/ML, And Investments In Cybersecurity And Multi-Cloud Strategies Are On The Rise At Small Businesses
    • November 9, 2023
  • OpenAI 5
    OpenAI Announces Leadership Transition
    • November 18, 2023
  • /
  • Technology
  • Tools
  • About
  • Contact Us

Input your search keywords and press Enter.