35

In the following regex what does "(?i)" and "?@" mean?

(?i)<.*?@(?P<domain>\w+\.\w+)(?=>)

I know that "?" means zero or one and that i sets case insensitivity.

This regex captures domains from an email address in a mailto field, but does not include the @ sign. It was generated the erex command from within SPLUNK 6.0.2

Deesbek
  • 865
  • 2
  • 12
  • 27
  • Are you sure you did not forget any char in the `(?P` part? E.g. `(?:P`? – sp00m Apr 09 '14 at 11:51
  • That definitely works as quoted. – Deesbek Apr 09 '14 at 11:53
  • @sp00m that is actually part of a named extraction (?P...). It could be specific to SPLUNK. – Deesbek Apr 09 '14 at 11:58
  • This is partially addressed in the [StackOverflow Regular Expression FAQ](http://stackoverflow.com/a/22944075/2736496). Relevant questions: [`*?`:zero-or more with reluctant quantifier](http://stackoverflow.com/a/10764399) (section "Quantifiers", at the top), and [What does the `i` modifier mean?](http://stackoverflow.com/a/12411066), (section "Modifiers", about 1/2 down). – aliteralmind Apr 09 '14 at 13:14
  • Splunk is also listed under "General documentation > Official documentation for specific flavors", about 3/4 down. – aliteralmind Apr 09 '14 at 13:20

2 Answers2

50

demo here : https://regex101.com/r/hE9gB4/1

(?i)<.*?@(?P<domain>\w+\.\w+)(?=>)

its actually getting your domain name from the email id:

(?i) makes it match case insensitive and

?@ is nothing but @ which matches the character @ literally.

the ? in your ?@ is part of .*? which we call as a lazy operator, It will give you the text between the < and @

if you dont use the ? after the .* it will match everything after < to the end. ( we call this as the greedy operator)

aelor
  • 10,892
  • 3
  • 32
  • 48
4

? here is the UNGREEDY or LAZYNESS modifier:

.*?

It means: "everything is good until the @ character that follows is detected". Otherwise .* would match everything until the end of the string.

Read about it here: http://www.regular-expressions.info/repeat.html

Cagy79
  • 1,610
  • 1
  • 19
  • 25