router7
router7

Reputation: 67

Trying to match text via REGEX

First time posting, and need a little help with a regex match I'm trying to come up with.

Here is the format I have

|user|DOMAIN\user|

I'm trying to write a regex match that will only capture the username, and then only capture the domain.

My thought process is to first match all the text and then create (3) groups? based on that I would have the information I'm looking for.

Suggestions? Examples?

Upvotes: 0

Views: 88

Answers (4)

Blindy
Blindy

Reputation: 67380

You're looking for a regex like this:

\|([^|]+)\|([^\]+)\\([^|]+)\|

Upvotes: 1

vishnu
vishnu

Reputation: 21

This is also another option for matching the username and domain.

% set data {|glenn|JACKMAN\glenn|}

|glenn|JACKMAN\glenn|

% regexp {^\|([^|]+)\|([^\]+).*} $data m a b

1

% puts "--- a $a ----b $b----"

--- a glenn ----b JACKMAN----

Another way for exact match.

% regexp {^\|([A-z]+)\|([A-z]+)\.*} $data m a b

1

% puts "match $m--- a $a ----b $b----"

match |glenn|JACKMAN\glenn|--- a glenn ----b JACKMAN----

Upvotes: 0

glenn jackman
glenn jackman

Reputation: 246827

Using non-greedy quantifiers helps a lot in readability here:

set data {|glenn|JACKMAN\glenn|}
regexp  {^\|(.*?)\|(.*?)\\} $data -> user domain
puts "$user:$domain"

outputs

glenn:JACKMAN

Upvotes: 1

alpha_ulrich
alpha_ulrich

Reputation: 556

If you do not have a constraint to use regex match, I would suggest that you go with splitting the string at pipes. This way, your first or second token (depending on how the given language treats an empty token before first pipe) will be user. Now you can remove the [length(user) + 1] substring from end of the next token. This would provide you with the DOMAIN.

Note that the assumption here is, that user or DOMAIN do not contain any pipe character (|).

Upvotes: 0

Related Questions