添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I want to write a regex that will match if and only if the pattern is not preceded by the characters "Etc/".

Strings that should match:

GMT+01:00
UTC+01:00
UTC+01
+01:00

Strings that should not match:

Etc/GMT+01:00
Etc/UTC+01:00
Etc/UTC+01

This is what I have so far:

(?<!Etc\/)((UTC|GMT)?(\+|\-){1}(\d{1,2})(:|\.)?(\d{1,2})?)

The right part of the above regular expression already matches the UTC and GMT offset and covers all the cases I need. But I don't manage to implement the exceptions mentioned above. I expected the above regex to not match the string Etc/GMT+1:00. But in fact it matches the part +01:00 and only ignores Etc/GMT.

How can I achieve that the the following regular expression does not match if it is preceded with "Etc/"?

(UTC|GMT)?(\+|\-){1}(\d{1,2})(:|\.)?(\d{1,2})?

Here I have an example with most of the use cases I need.

You may add \S* after Etc/ to make sure Etc/ is checked even if there are any zero or more non-whitespace chars between Etc/ and the expected match:

(?<!\bEtc/\S*)((UTC|GMT)?([+-])(\d{1,2})[:.]?(\d{1,2})?)

See the .NET regex demo

Details:

  • (?<!\bEtc/\S*) - a negative lookbehind that matches a location that is not immediately preceded with a whole word Etc/ and then zero or more non-whitespace chars
  • (UTC|GMT)? - an optional substring, UTC or GMT
  • ([+-]) - + or -
  • (\d{1,2}) - one or two digits
  • [:.]? - an optional : or .
  • (\d{1,2})? - an optional sequence of one or two digits (equal to (\d{0,2})).
  • As you are already capturing all data in groups, another way could be getting all the matches of Etc/ out of the way, and use your pattern to capture what you want in the groups.

    Note that you can change groupings of single chars like (:|\.) to a character class ([:.])

    \bEtc/\S*|(UTC|GMT)?([+-])(\d{1,2})([:.])?(\d{1,2})?
    
  • \bEtc/\S* Match Etc/ and optional non whitespace chars
  • (UTC|GMT)?([+-])(\d{1,2})([:.])?(\d{1,2})? Your pattern with all the separate groups.
  • Regex demo

    Or with just a single group:

     \bEtc/\S*|((?:GMT|UTC)?\+\d{2}(?:[:.]\d{2})?)
    

    Regex demo

    Thanks for contributing an answer to Stack Overflow!

    • Please be sure to answer the question. Provide details and share your research!

    But avoid

    • Asking for help, clarification, or responding to other answers.
    • Making statements based on opinion; back them up with references or personal experience.

    To learn more, see our tips on writing great answers.