Regular pattern with Kubernetes cluster endpoint utilizing either IP address or fully qualified domain name

In my Angular/typescript project, I am working on building a regex for a cluster endpoint that includes an IP address or hostname (FQDN) in a URL format. For instance:

Example 1 - 10.210.163.246/k8s/clusters/c-m-vftt4j5q

Example 2 - fg380g9-32-vip3-ocs.sample.company.com/k8s/clusters/c-m-8vcjbtwh

(Please note there should not be a slash at the end)

I have combined two different regular expressions: one for IP/FQDN and another for matching the remaining part of the URL like "/k8s/clusters/c-m-vftt4j5q". Here are the regex patterns:

First regex pattern for IP/FQDN: ^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$|^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)+([A-Za-z]|[A-Za-z][A-Za-z0-9\-]*[A-Za-z0-9])$
Second regex pattern for the rest of the URL: ^\/(([a-zA-Z0-9-]+)\/)+([a-zA-Z0-9-]+)$
Combined regex pattern:
^((?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$|^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)+([A-Za-z]|[A-Za-z][A-Za-z0-9\-]*[A-Za-z0-9]))[\/]{1}(([a-zA-Z0-9-]+)\/)+([a-zA-Z0-9-]+)$

However, this combined regex matches with Example 2 but not Example 1. What modifications should I make to correct this?

Answer №1

Adjusting it to meet your specific needs.
I have made slight modifications to your last regex pattern
to accommodate both test cases.

/^((?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)|(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9-]*[a-zA-Z0-9])\.)+([A-Za-z][A-Za-z0-9-]*[A-Za-z0-9]))\/(([a-zA-Z0-9-]+)\/)+([a-zA-Z0-9-]+)$/

https://regex101.com/r/zT32K6/1

RegexPattern :

^ 
(                                               # (1 start)
  (?:
    (?:
      25 [0-5] 
    | 2 [0-4] [0-9] 
    | [01]? [0-9] [0-9]? 
    )
    \. 
  ){3}
  (?:
    25 [0-5] 
  | 2 [0-4] [0-9] 
  | [01]? [0-9] [0-9]? 
  )
| (                                               # (2 start)
    (                                               # (3 start)
      [a-zA-Z0-9] 
    | [a-zA-Z0-9] [a-zA-Z0-9-]* [a-zA-Z0-9] 
    )                                               # (3 end)
    \. 
  )+                                              # (2 end)
  ( [A-Za-z] [A-Za-z0-9-]* [A-Za-z0-9] )          # (4)
)                                               # (1 end)
/
(                                               # (5 start)
  ( [a-zA-Z0-9-]+ )                               # (6)
  /
)+                                              # (5 end)
( [a-zA-Z0-9-]+ )                               # (7)
$

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Generating a comprehensive url() entry utilizing regular expressions

I didn't encounter any issues with my Django and React URL routing during development, but as I attempted to transition to production, a multitude of problems arose. Admittedly, regex is not my strong suit. It appears as though a feline pranced acros ...

Numerous social media sharing buttons conveniently located on a single webpage

Currently, I am working on a research database project where the goal is to allow users to share articles from the site on various social networks such as Facebook, Twitter, LinkedIn, and Google+. To achieve this, I successfully implemented share buttons ...

Is it possible to make my Toggle/Click event refresh the entire component every time it is clicked?

I'm trying to implement a toggle function to show/hide a specific DIV and dynamically change the button text based on the current state in React Hooks. However, every time I click on it, the entire page seems to re-render in Next.js. I'm not enti ...

Ever since updating my Node JS, the functionality of the MaterializeCSS carousel methods has ceased to work

Recently, I encountered some issues with my React project that features a materialize-css carousel. The problem arose after updating my nodeJS version from 14.8.1 to 16.13.0. Specifically, these errors kept popping up: TypeError: Cannot read properties o ...

Different JQuery countdowns in an iteration using Django

I'm in the process of developing a sports app using Django. One of the key features I want to include is the ability to display a list of upcoming matches with a countdown timer for each match. Currently, I have managed to implement a single countdow ...

Struggling to find your way around the header on my website? Let me give

I'm looking to display certain links prominently at the top of a page, similar to this example: "home > page1 > link1 > article 1." Can someone advise on how to achieve this using HTML, PHP, or jQuery? ...

Dynamic form validation using jQuery

I am facing a challenge with validating a dynamic form on the user side. My goal is to ensure that if a user fills out one column in a row, they are required to fill out the remaining columns as well. For example, filling out the CC # should prompt the use ...

Having trouble with preventDefault() not working during a keyup event?

I am struggling to make preventDefault() function properly. Here are a few variations of the code that I have attempted: First: $(document).keyup(function (evt) { var charCode = (evt.which) ? evt.which : event.keyCode; if (charCode == 192) { ...

Error message: "Encountered 'Cannot Get' error while building Angular

I encountered an error when trying to run my Angular project in IntelliJ. Upon opening the browser on localhost, I received the message Cannot GET / Here are the steps I followed: Opened up the project Ran npm install -g @angular/cli@latest Ran gradlew c ...

Tips for managing erroneous data in csv files using Node.js

I am currently working on an application that parses csv files using the csv module. It is functioning well, but I am facing a problem where if there is a bad row in the csv file, the entire process fails. Is there a way to skip bad rows and continue stre ...

Using additional properties when referencing another Mongoose schema

var UserSchema = new Schema({ job : [{ type : mongoose.Schema.Types.ObjectId, experience : String, grade : String, ref: 'Company'}] }); User's profile can include multiple jobs. The process of adding a job to the user is a ...

Developing Your Own Local Variable in Angular with Custom Structural Directive ngForIn

I am hoping for a clear understanding of this situation. To address the issue, I developed a custom ngForIn directive to extract the keys from an object. It functions correctly with the code provided below: import {Directive, Input, OnChanges, SimpleChan ...

When implementing Critical CSS, the Jquery function fails to execute upon loading

Currently, I am working on optimizing my website at . In an attempt to improve its performance, I decided to implement critical CSS using penthouse. The Critical CSS code can be found here, generously provided by the main developer of penthouse. However, ...

Comprehending the concept of TypeScript type assertion

Currently, I'm learning TypeScript and I came across a code snippet that is causing some confusion. var str = '1' var str2:number = <number> <any> str //str is now of type number console.log(typeof(str2)) log: String From m ...

Unable to utilize ngForm when values are already predefined

I have an Angular application with Ionic 4. Here is the HTML code for my form: <form #formAuth="ngForm" (ngSubmit)="sendCode(formAuth)" method="post"> <ion-select placeholder="Country" ngModel name="area_code" interface="modal"> <io ...

What are the steps to transition from @zeit/next-sass deprecation?

Is there a way to transition and modify the next.config.js file to switch from using @zeit/next-sass to leveraging Next.js's built-in support for Sass? Check out this link for more information: https://www.npmjs.com/package/@zeit/next-sass const withS ...

Using AngularJS to automatically include predefined data in an $http request

I am looking for a way to include a default data attribute in an http request. I came up with the following code: var app = angular.module('dumbhttp', []); app.service('dumbhttp', function ($http, $location) { this.post = function ...

CoffeeScript is failing to run the code

I'm attempting to use a click function to alter the CSS code and then run a function. Here is my current code: ready: -> $("#titleDD").click -> $("#titleDD").css('text-decoration', 'underline'); $("#catDD").css( ...

Retrieve the text value of a selected option in a v-select component

After reading some documentation about v-select and slots, I am a bit confused about whether it can be applied to my specific scenario on this codepen. All I really need is to capture the selected text (not the value) and utilize it somewhere in the code: ...

I'm encountering a Typescript error where I'm unable to assign a function to RefObject.current and it's indicating that the function is not callable

Does anyone know why assigning a function type to a ref.current type is causing me issues? useEffect(() => { savedHandler.current = handler; // ERROR HERE: }, [handler]); TS2741: Property 'current' is missing in type '(e: Chang ...