(\d{1,3}\.){3}\d{1,3}:\d{2,5} You can make it so that arbitrary text is added to the expression found on this regex . For example:
127.0.0.1:8888@blalabla:blablabla Not like now:
127.0.0.1:8888 (\d{1,3}\.){3}\d{1,3}:\d{2,5} You can make it so that arbitrary text is added to the expression found on this regex . For example:
127.0.0.1:8888@blalabla:blablabla Not like now:
127.0.0.1:8888 Regular expressions do not change the text by themselves. Most engines have a text replacement operation, for example, s/// in Perl, re.sub() in Python.
To add text after the pattern, you can use \K : s/шаблон\K/добавка/ add s/шаблон\K/добавка/ , for example, to add @bla after ip:port in the input text:
$ echo 'a 127.0.0.1:8888 z' | perl -pe 's/(?:\d{1,3}\.){3}\d{1,3}\:\d{2,5}\K/\@bla/g' Result:
a 127.0.0.1:8888@bla z If the regular expression library in this language does not support \K , then you can use group capture (using () ) and refer to the group using $1 , \1 depending on the regexp dialect. Example on Python:
$ echo 'a 127.0.0.1:8888 z' | python -c 'import re, sys; print(re.sub(r"((?:\d{1,3}\.){3}\d{1,3}\:\d{2,5})", r"\1@bla", sys.stdin.read()))' The same result:
a 127.0.0.1:8888@bla z Since \K not used, it is necessary to add \1 to the replacement expression, which inserts the captured text group.
Try this
(\d{1,3}\.){3}\d{1,3}:\d{2,5}.* Source: https://ru.stackoverflow.com/questions/397357/
All Articles