Skip to main content
Example scripts
arrow icon
To homepage
Jira
Data centre icon
Data Center

Use Regex to Find Email Addresses in a String and Find the User

Created 1 year ago, Updated 1 month(s) ago
App in script
ScriptRunner For Jira
ScriptRunner For Jira
by Adaptavist
Compatibility
compatibility bullet
Jira (8.14 - 9.5)
compatibility bullet
ScriptRunner For Jira (7.7.0)
Language |
groovy
import java.util.regex.Pattern
import com.atlassian.jira.component.ComponentAccessor

//Define the UserSearchService, which we will use to find the User from the Email Address
def userSearchService = ComponentAccessor.userSearchService

//Define the Regex to find the Email Addresses
def regex = "([a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\\.[a-zA-Z0-9_-]+)"
//Define a String to search through
def searchQuery = "this is the email: example@email.com, anotherExample@email.com"

//Define the Pattern with the Regex, and the Matcher with the searchQuery
def pattern = Pattern.compile(regex, Pattern.MULTILINE)
def matcher = pattern.matcher(searchQuery)

//Using each, iterate through the Matcher's result
matcher.each {
    //Observe the Email Address found
    log.warn matcher.group(0)

    //Use the UserSearchService to find the User by Email
    def userFoundByEmail = userSearchService.findUsersByEmail( matcher.group(0) )
    //Observe the User found - if this returns an empty List, no User was found
    log.warn userFoundByEmail
}
Having an issue with this script?
Report it here