I have a string that looks like this:
a.*.AGG.all:some__string_here{location=*,username=fred.nurk__nurk.com,testId=12345}
When our application interprets this string, each item with the {} becomes a tag which can be referenced.
Therefore you will have tags for location, username and testId.
Using this regex, (?<=username=)([w.]+)
, I can capture fred.nurk__nurk.com
in capture group 1.
What I have not been able to figure out is how to then replace the __ in capture group 1 with @.
The end result should see the tag username = [email protected]
Thank you
PS. If you have a better solution, I would be most appreciative.
3
Try:
(?<=username=)((?:[a-zA-Z0-9.]|_(?!_))*)(__)*((?:[a-zA-Z0-9.]|_(?!_))*)
and replacing with:
$1@$2
See: regex101
Explanation
MATCH:
(?<=username=)
: Anchors to the username field( ... )
: Capture to group 1(?: ... )*
: repeatedly[a-zA-Z0-9.]
:[w.]
but no_
|
: or_(?!_)
:_
but not two__
(__)*
: match __ to group 2((?:[a-zA-Z0-9.]|_(?!_))*)
: To group 3 with same logic as group 1
REPLACE if it is always an E-Mail address:
$1
: Keep group 1@
: substitute__
for@
$3
: Keep group 2
REPLACE if you can do conditional replacement:
$1
: Keep group 1${2:+@:}
: substitute group 2, when set with@
else leave blank$3
: Keep group 2
3
It depends on which language you’re using. For example, in python:
import re
st = "a.*.AGG.all:some__string_here{location=*,username=fred.nurk__nurk.com,testId=12345}"
print(re.sub(r"(?<=username=)([w.]+)", lambda m: m.group(1).replace("__", "@"), st))
Result:
a.*.AGG.all:some__string_here{location=*,[email protected],testId=12345}
aaa is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
3