Home > Blockchain >  Regex to match the string "Hello, name"
Regex to match the string "Hello, name"

Time:12-31

I need a regex which can match "Hello, name" pattern but the comma is optional. If the comma is not present, the number of spaces between Hello and name should be one. If the comma is present, then the number of spaces between comma and name should be one. After Hello (comma space or space), there should at least be one character and after that anything can follow

I tried the following regex

Hello,?\s{1}\S.*

But it also matches "Hello , name" i.e space between Hello and comma.

CodePudding user response:

The reason Hello,?\s{1}\S.* matches "Hello , name" (space after Hello) is:

  • Hello matches the initial "Hello".
  • Since the comma is optional, it is not matched
  • \s{1} matches the space after the "Hello" (Note the {1} is redundant. \s on its own will do the same thing, match a single whitespace)
  • \S matches the comma
  • .* matches the rest of the string, i.e. " name"

To prevent this, consider disallowing commas and spaces in the name Try online:

^Hello,?\s[^,\s] $
------------------
^                $      : Start and end of string
 Hello                  : Literal Hello
      ,?                : Optional comma
        \s              : One whitespace
          [^,\s]        : One or more characters that are not comma or space
import re

strings = """Hello, Chopin
Hello Brahms
Hello,  Mozart
Hello , name
Hello  name
Hello Chopin""".split("\n")

for test in strings:
    print(test, re.search(r"^Hello,?\s[^,\s] $", test))

gives:

Hello, Chopin <re.Match object; span=(0, 13), match='Hello, Chopin'>
Hello Brahms <re.Match object; span=(0, 12), match='Hello Brahms'>
Hello,  Mozart None
Hello , name None
Hello  name None
Hello Chopin <re.Match object; span=(0, 12), match='Hello Chopin'>

CodePudding user response:

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

The regex: (Hello,?)(\s?)[a-zA-Z]* seems to do it, below the explanation from regex101.com:

/ (Hello,?)(\s?)[a-zA-Z]* / gm

1st Capturing Group (Hello,?) Hello matches the characters Hello literally (case sensitive) , matches the character , with index 4410 (2C16 or 548) literally (case sensitive) ? matches the previous token between zero and one times, as many times as possible, giving back as needed (greedy)

2nd Capturing Group (\s?) \s matches any whitespace character (equivalent to [\r\n\t\f\v ]) ? matches the previous token between zero and one times, as many times as possible, giving back as needed (greedy)

Match a single character present in the list below [a-zA-Z]

  • matches the previous token between zero and unlimited times, as many times as possible, giving back as needed (greedy) a-z matches a single character in the range between a (index 97) and z (index 122) (case sensitive) A-Z matches a single character in the range between A (index 65) and Z (index 90) (case sensitive)

Global pattern flags g modifier: global. All matches (don't return after first match) m modifier: multi line. Causes ^ and $ to match the begin/end of each line (not only begin/end of string)

P.S. this will not work when the name has a space in it.

CodePudding user response:

You can use the regex, Hello,?\s\S.* which can be explained as follows:

  • Hello: The word, Hello
  • ,?: Optional comma
  • \s: Single whitespace character
  • \S: Single non-space character
  • .*: Any character any number of times

Regex demo

CodePudding user response:

This regular expression will match the string "Hello, name" even if there are other characters before or after it.

import re

pattern = r".*Hello, name.*"

# Test the regular expression
if re.match(pattern, "Hello, name"):
    print("Match found!")
else:
    print("Match not found.")
  • Related