简体   繁体   English

RegEx在字符串的开头或字符串本身的开头匹配符号

[英]RegEx match for a symbol at the beginning of a string, or the beginning of string itself

I'm trying to come up with a regex that would catch a TLD in a group of subdomains. 我正在尝试提出一个正则表达式,它将在一组子域中捕获一个TLD。 So I have this: 所以我有这个:

/\.?google\.com$/i

That should do the following matches: 那应该进行以下匹配:

sgoogle.com            -- no match
google.com             -- match
maps.google.com        -- match
job_s.map.google.com   -- match
place.google.com.eu    -- no match

This works, except for the first one, ie sgoogle.com that it matches as well, which it shouldn't. 除了第一个匹配的sgoogle.com ,它也应该匹配,但不匹配。

So I'm curious, is there a way to specify where I do \\.? 所以我很好奇,有没有办法指定我在哪里做\\.? to match either a dot or the beginning of string? 匹配点或字符串的开头?

You're almost there. 你快到了。 Literally, your requirement would be: 从字面上看,您的要求是:

(?:^|\.)google\.com$

But depending on your needs, this could be better (or not, you tell me): 但是根据您的需求,这可能会更好(或者,您告诉我,不是):

\bgoogle\.com$

The \\b means word boundary . \\b表示单词边界

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM