Regex Pattern to match the name ending in either # or >

what does the "[0-9]+" match in a regular expression?
regex match special characters
regex one or more digits
regex any number of digits
regular expression examples
regular expression match
what would the regular expression '\s+?@\s+' match?
regex any character

I have been trying to find a single regex pattern that will help me to match 4 different kind of cases from where I can retrieve the Hostname.

Case 1: HOSTNAME# Case 2: HOSTNAME> Case 3: myid8@HOSTNAME> Case 4: myid8@HOSTNAME#

For all the cases above I need to match only the Hostname, the Hostname can be anything;

so far I have tried to match "#" for case 1, ">" for case 2, trying to match the first "@" and ">" for case 3 and "@" and "#" for case 4.

This is what I have tried so far:

def testingregex(self):
    if "@" in self.device_name:
        m = re.search(r'@(.*)>|@(.*)#', self.device_name)
        print("Hostname on the Device is: %s" % m.group(1))
    else:
        m = re.search(r'^(.+?)#|^(.+?)>', self.device_name)
        try:
            print("Hostname on the Device is: %s" % m.group(1))
        except:
            print("Hostname on the Device is: %s" % m.group(2))

My file has all the four kinds of cases: I get this output: HOSTNAME None HOSTNAME None

You can use this regex,

[^@ ]+(?=[#>])

[^@ ]+ finds the hostname by excluding @ or a space in it capturing one or more characters and stops capturing when it makes sure it is followed by # or > using positive look ahead (?=[#>])

Demo

Sample Python codes,

import re

arr = ['HOSTNAME#','HOSTNAME>','myid8@HOSTNAME>', 'myid8@HOSTNAME#']

for s in arr:
 m = re.search(r'[^@ ]+(?=[#>])', s)
 if (m):
  print(s, ' --> ', m.group())

Prints,

HOSTNAME#  -->  HOSTNAME
HOSTNAME>  -->  HOSTNAME
myid8@HOSTNAME>  -->  HOSTNAME
myid8@HOSTNAME#  -->  HOSTNAME

MySQL Reference Manual: Documentation from the Source, [ a - z ] matches any lowercase letter , whereas [ 0 - 9 ] matches any digit . To anchor a pattern so that it must match the beginning or end of the value being tested of the name : mysql > SELECT * FROM pet WHERE name REGEXP " b " ; +  When attempting to build a logical “or” operation using regular expressions, we have a few approaches to follow. Fortunately the grouping and alternation facilities provided by the regex engine are very capable, but when all else fails we can just perform a second match using a separate regular expression – supported by the tool or native language of your choice.

SQL in easy steps, 3rd edition: For web developers, programmers , Support for regular expressions does vary from one DBMS to another. a letter “​A” in either uppercase or lowercase. match-regexp.sql SQL Matching regular WHERE name REGEXP "^B"; # Show records where the name ends with a "H". In regex, anchors are not used to match characters. Rather they match a position i.e. before, after, or between characters. To match start and end of line, we use following anchors: Caret (^) matches the position before the first character in the string. Dollar ($) matches the position right after the last character in the string. 2.

Regular expression examples, In this example, you use the regular expression to extract a host name from the The outcome is a match of all zones beginning with S that are followed by any a hostname (myComputerName in the example) that ends with either an A, a B,  A Regular Expression (RegEx) is a sequence of characters that defines a search pattern. For example, ^as$ The above code defines a RegEx pattern. The pattern is: any five letter string starting with a and ending with s. A pattern defined using RegEx can be used to match against a string.

1.4. Regular Expressions, Below follows a list of most commonly used regular expressions together with explanations (the dot) means "any character", e.g. "b.ng" will match "bang", "​b8ng", but not "baang" \b, at the beginning and/or end of a string, word boundary. Regex Matching is available: in the If condition of an action, when the ~R or !~R operators are specified. in the Variable Search Replace action in the condition of a Variable Value state wherever a Simple Match is possible, by preceding the regex with ~R or !~R. Matching Rules Standard Java regular expression matching is applied.

Regular Expression (Regex) Tutorial, Regular Expression, or regex or regexp in short, is extremely and amazingly \B: Inverse of \b, i.e., non-start-of-word or non-end-of-word. This regex matches any numeric substring (of digits 0 to 9) of the input. w+)* is used to match the email domain name, with the same pattern as the username described above. A regular expression (shortened as regex or regexp; also referred to as rational expression) is a sequence of characters that define a search pattern.Usually such patterns are used by string searching algorithms for "find" or "find and replace" operations on strings, or for input validation.

Chapter 17 Using Regular Expressions, A regular expression, often called regex, is a way of describing patterns in strings To illustrate, consider the baby names data, summarised to give the total For example, ".ble" is a regex that matches any set of four letters ending with “ble”. A regular expression is used to check if a string matches a pattern or not. A regular expression or regex or regexp is a sequence of characters that defines a pattern. A pattern may consist of literals, numbers, characters, operators, or constructs. The pattern is used to search strings or files to see if matches are found.

Comments
  • Hostname can be anything including spaces?
  • It won't have any white space, but it can have "-", numbers, or any other character possible