Home > OS >  How do I match a colon with no values after it?
How do I match a colon with no values after it?

Time:07-27

I'm new to the website and to Regular Expression as well. So I want to bookmark a list of Emails that have no value after the colons ":" as highlighted in the picture below.

Link

Here is an example:

[email protected]:123456
[email protected]:test123#@NEW
[email protected]:
[email protected]:

I only want to bookmark the last two ones so it would be like this:

[email protected]:
[email protected]:

CodePudding user response:

If you only want to find strings that end with a colon, then all you need is :$.

CodePudding user response:

I find this request a bit odd, perhaps if you could elaborate a bit more on your use case I may be able to provide a better approach or solution.

Now, I think that this expression should work the way you expect:

[\w\.] @[a-z0-9][a-z0-9-]*[a-z0-9]?

Add the colon sign at the end if you need to match for the colon sign as well.

I noticed that the other proposed expressions don't account for email addresses with a dot in the username part or with dashed in the domain part. You may use a combination of all the solutions if you are more familiar with RegEx. I highly recommend you test the expression before moving it to production, you can do further tests easily on this page https://regexr.com/.

[a-z0-9!#$%&'* /=?^_`{|}~-] (?:\.[a-z0-9!#$%&'* /=?^_`{|}~-] )*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?) [a-z0-9](?:[a-z0-9-]*[a-z0-9])?

Will be a more adequate RegEx since the Internet Engineering Task Force established limits on how an email address can be formatted and this accounts for those additional characters. More details on this page https://www.mailboxvalidator.com/resources/articles/acceptable-email-address-syntax-rfc/.

As a friendly reminder, Stack Overflow can be best used when you have already invested some effort in fixing some problem, rather than having a community member provide you with a straight answer. This and other suggestions are listed on this other page https://stackoverflow.com/tour.

CodePudding user response:

Try this:

[a-zA-Z] @[a-zA-Z] : # Only a-zA-Z, numbers are not accepted

Note: the last character is a space " "

[\w ] @[\w ] : # \w  = Matches one or more [A-Za-z0-9_]

Without a space it will matches only these with no character after the colon.

[\w ] @[\w ] .*:$ # Matches only when there is also .XXX. For example: .com or .de

CodePudding user response:

Given this:

[email protected]:test123#@NEW

There are three parts to this:

  1. Before the @.
  2. Between the @ and the :
  3. After the :

If we assume (1) has to be there and not empty.
If we assume (2) has to be there and not empty.
If we assume (3) the ':' is required by the trailing part can be empty.
I don't want to make assumptions about other requirements.

Then I would use:

[^@] @[^:] :.*$

Meaning:

[^@]     => Anything apart from the '@' character.
[^@]     => The above 1 or more times.
[^@] @   => The above followed by '@' character.

[^:]     => Anything apart from the ':' character.
[^:]     => The above 1 or more times.
[^:] :   => The above followed by ':' character.

.*       => Any character 0 or more times.

$        end of line.

So if we want to mkae sure we only find things that don't have anything after the ':' we can simplify a bit.

    [^@] @[^:] :$

Make sure we have the '@' and ':' parts and they are none empty. But the colon is followed by the end of line.

If you don't care about part (1) or (2) we can simplify even more.

    [^:] :$

Line must contain a : don't care what is in front as long as there is a least one character before the ':' and zero after.

Final simplification.

    :$

If you don't care about anything except that the colon is not followed by anything.

CodePudding user response:

The following regex will match the "pre-colon" pattern if and only if it is followed by nothing but whitespace until the end of the line:

\w @\w \.\w :\s*$

View on regex101

Note that matching email addresses with 100% correctness is more complicated than this, but this will likely do for your use case.

  • Related