Sed command with regular expression and replace [closed] - regex

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I am new to sed command and regular expressions.
Could anyone help me to explain the below command
echo "FIXTEST_XYZ 23040/tcp # 127.0.0.1:23040" | sed -n 's/^FIXTEST_\([A-Za-z0-9]\+\)\s\+\([0-9]\+\)\/tcp\s\+#\s*\([A-Za-z0-9.]\+\):\([0-9]\+\)\s*/\1 \2 \3 \4/p'
Thank you for your help !!

Check explainshell for command explaination.
Check regex101 for regex explanation:
^FIXTEST_([A-Za-z0-9]+)\s+([0-9]+)\/tcp\s+#\s*([A-Za-z0-9.]+):([0-9]+)\s*
FIXTEST_ matches the characters FIXTEST_ literally (case sensitive)
1st Capturing Group ([A-Za-z0-9]+)
Match a single character present in the list below [A-Za-z0-9]+
+ Quantifier — Matches between one and unlimited times, as many times as possible, giving back as needed (greedy)
A-Z a single character in the range between A (index 65) and Z (index 90) (case sensitive)
a-z a single character in the range between a (index 97) and z (index 122) (case sensitive)
0-9 a single character in the range between 0 (index 48) and 9 (index 57) (case sensitive)
\s+ matches any whitespace character (equal to [\r\n\t\f\v ])
+ Quantifier — Matches between one and unlimited times, as many times as possible, giving back as needed (greedy)
2nd Capturing Group ([0-9]+)
Match a single character present in the list below [0-9]+
+ Quantifier — Matches between one and unlimited times, as many times as possible, giving back as needed (greedy)
0-9 a single character in the range between 0 (index 48) and 9 (index 57) (case sensitive)
\/ matches the character / literally (case sensitive)
tcp matches the characters tcp literally (case sensitive)
\s+ matches any whitespace character (equal to [\r\n\t\f\v ])
+ Quantifier — Matches between one and unlimited times, as many times as possible, giving back as needed (greedy)
# matches the character # literally (case sensitive)
\s* matches any whitespace character (equal to [\r\n\t\f\v ])
3rd Capturing Group ([A-Za-z0-9.]+)
: matches the character : literally (case sensitive)
4th Capturing Group ([0-9]+)
\s* matches any whitespace character (equal to [\r\n\t\f\v ])
Global pattern flags
g modifier: global. All matches (don't return after first match)
1st Capturing Group matches: XYZ
2nd Capturing Group matches: 23040
3rd Capturing Group matches: 127.0.0.1
4th Capturing Group matches: 23040
Bash standard output: XYZ 23040 127.0.0.1 23040

Related

What is the output of this regex [duplicate]

This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 3 years ago.
What is the output of this regular expression?
/\s+(\d+)\s+/
In particular what is the meaning of /\s
In your regex \s+ matches any number of whitespaces sequentially and /d+ matches any number of digits sequentially .
\s and \d matches a single whitespace and single digit respectively the + makes it match any number of sequential whitespaces and digits respectively.
You can find a full explanation at regex101.com.
/\s+(\d+)\s+/
\s+ matches any whitespace character (equal to [\r\n\t\f\v ])
+ Quantifier — Matches between one and unlimited times, as many times as possible, giving back as needed (greedy)
1st Capturing Group (\d+)
\d+ matches a digit (equal to [0-9])
+ Quantifier — Matches between one and unlimited times, as many times as possible, giving back as needed (greedy)
\s+ matches any whitespace character (equal to [\r\n\t\f\v ])
+ Quantifier — Matches between one and unlimited times, as many times as possible, giving back as needed (greedy)
https://regex101.com/
might be useful :D
\s+(\d+)\s+ / ↵ matches the character
↵ literally (case sensitive)
\s+
matches any whitespace character (equal to [\r\n\t\f\v ])
+ Quantifier — Matches between one and unlimited times, as many times as possible, giving back as needed (greedy) 1st Capturing Group
(\d+)
\d+ matches a digit (equal to [0-9])
+ Quantifier — Matches between one and unlimited times, as many times as possible, giving back as needed (greedy)
\s+ matches any whitespace
character (equal to [\r\n\t\f\v ])
+ Quantifier — Matches between one and unlimited times, as many times as possible, giving back as needed

How to match string starting from multiple characters (can be nested) via regex?

Im trying to make regex to get domain from different kinds of url.
Im using regex, that works properly either with links w/o # in domain part, e.g:
https://stackoverflow.com/questions/ask
https://regexr.com/
/(?<=(\/\/))[^\n|\/|:]+/g
For links with # (e.g. http://regex#regex.com) works with replasing \/\/ to \#:/(?<=(#))[^\n|\/|:]+/g
But when im trying to make regex to match both of these cases and making
/(?<=((\/\/)|(\#)))[^\n|\/|:]+/g
it dosen't work.
You should look for the string ://,(Positive Look Behind) if it comes in the string means it is domain and you need to capture everything after that. Whether it has # or not.
Case 1
Capture the whole string after ://
Regex:
(?<=\:\/\/).*
Explanation:
Positive Lookbehind (?<=\:\/\/)
Assert that the Regex below matches
\: matches the character : literally (case sensitive)
\/ matches the character / literally (case sensitive)
\/ matches the character / literally (case sensitive)
.* matches any character (except for line terminators)
* Quantifier — Matches between zero and unlimited times, as many times as possible, giving back as needed (greedy)
Example
https://regex101.com/r/jsqqw8/1/
Case 2
Capture just the domain after ://
Regex:
(?<=:\/\/)[^\n|\/|:]+
Explanation:
Positive Lookbehind (?<=:\/\/)
Assert that the Regex below matches
: matches the character : literally (case sensitive)
\/ matches the character / literally (case sensitive)
\/ matches the character / literally (case sensitive)
Match a single character not present in the list below [^\n|\/|:]+
+ Quantifier — Matches between one and unlimited times, as many times as possible, giving back as needed (greedy)
\n matches a line-feed (newline) character (ASCII 10)
| matches the character | literally (case sensitive)
\/ matches the character / literally (case sensitive)
|: matches a single character in the list |: (case sensitive)
Case 3:
Capture domain after :// if there is not # in the text and if # present in the text, capture text after that.
Regex:
(?!:\/\/)(?:[A-z]+\.)*[A-z][A-z]+\.[A-z]{2,}
Explanation:
Negative Lookahead (?!:\/\/)
Assert that the Regex below does not match
: matches the character : literally (case sensitive)
\/ matches the character / literally (case sensitive)
\/ matches the character / literally (case sensitive)
Non-capturing group (?:[A-z]+\.)*
* Quantifier — Matches between zero and unlimited times, as many times as possible, giving back as needed (greedy)
Match a single character present in the list below [A-z]+
+ Quantifier — Matches between one and unlimited times, as many times as possible, giving back as needed (greedy)
A-z a single character in the range between A (index 65) and z (index 122) (case sensitive)
\. matches the character . literally (case sensitive)
Match a single character present in the list below [A-z]
A-z a single character in the range between A (index 65) and z (index 122) (case sensitive)
Match a single character present in the list below [A-z]+
+ Quantifier — Matches between one and unlimited times, as many times as possible, giving back as needed (greedy)
A-z a single character in the range between A (index 65) and z (index 122) (case sensitive)
\. matches the character . literally (case sensitive)
Match a single character present in the list below [A-z]{2,}
{2,} Quantifier — Matches between 2 and unlimited times, as many times as possible, giving back as needed (greedy)
A-z a single character in the range between A (index 65) and z (index 122) (case sensitive)
Example:
https://regex101.com/r/jsqqw8/4

BASH -- grep'ing for perl-regex [duplicate]

This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 5 years ago.
Beginner here and I'm trying to understand this. Can someone please break down the part in between the single quotes and describe what it does?
grep -oP '(?<=\S\/1\.\d.\s)[345]\d+'
Many thanks in advance!
Positive Lookbehind (?<=\S/1.\d.\s) Assert that the Regex below matches
\S matches any non-whitespace character (equal to [^\r\n\t\f\v ])
\/ matches the character / literally (case sensitive)
1 matches the character 1 literally (case sensitive)
\. matches the character . literally (case sensitive)
\d matches a digit (equal to [0-9])
. matches any character (except for line terminators)
\s matches any whitespace character (equal to [\r\n\t\f\v ])
Match a single character present in the list below [345]
345 matches a single character in the list 345 (case sensitive)
\d+ matches a digit (equal to [0-9])
+ Quantifier — Matches between one and unlimited times, as many times as possible, giving back as needed (greedy)
Output simply copied from https://regex101.com/r/HfJSNm/1 : very handy to test/share/have automatic explications on regexes.

Notepad++. Remove all spaces into braces [] [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
How can I remove all spaces into braces with Notepad++ and RegEx?
For example:
I have string [Word1 Word2 Word3]
I need: [Word1Word2Word3]
Thanks
\s++(?=[^[]*])
\s++
matches any whitespace character (equal to [\r\n\t\f\v ])
++ Quantifier — Matches between one and unlimited times, as many times as possible, without giving back (possessive)
Positive Lookahead (?=[^[]*])
Assert that the Regex below matches
Match a single character not present in the list below [^[]*
* Quantifier — Matches between zero and unlimited times, as many times as possible, giving back as needed (greedy)
[ matches the character [ literally (case sensitive)
] matches the character ] literally (case sensitive)
(?:\[|\G(?!^))[^]\s]*\K\s+
Non-capturing group (?:\[|\G(?!^))
1st Alternative \[
\[ matches the character [ literally (case sensitive)
2nd Alternative \G(?!^)
\G asserts position at the end of the previous match or the start of the string for the first match
Negative Lookahead (?!^)
Assert that the Regex below does not match
^ asserts position at start of the string
Match a single character not present in the list below [^]\s]*
* Quantifier — Matches between zero and unlimited times, as many times as possible, giving back as needed (greedy)
] matches the character ] literally (case sensitive)
\s matches any whitespace character (equal to [\r\n\t\f\v ])
\K resets the starting point of the reported match. Any previously consumed characters are no longer included in the final match
\s+
matches any whitespace character (equal to [\r\n\t\f\v ])
+ Quantifier — Matches between one and unlimited times, as many times as possible, giving back as needed (greedy)

regex: extract text blocks, defined beginning, undefined end

i have text like this:
Date: 01.02.2015 //<-stable format
something
something more
some random more
Date: 02.02.2015
something random
i dont know
so i have many such blocks. Starts with Date... ends with next Date... start.
The text in the lines in the block could be anything, but not Date... format
I need an array at the end, with such blocks:
array[0] = "Date: 01.02.2015
something
something more
some random more"
array[1] = "Date: 02.02.2015
something random
i dont know"
for now i add some unique splitter before Date... than split by the splitter.
Question: is it possible to get such blocks only by regex?
(i use VBA to parse the text, RegExp object)
Instead of split just match using
\bDate:\s\d{1,2}\.\d{1,2}\.\d{4}[\s\S]*?(?=\nDate:|$)
See demo.
https://regex101.com/r/uF4oY4/77
Syntax explanation (from the linked site):
\b assert position at a word boundary: (^\w|\w$|\W\w|\w\W)
Date: matches the characters Date: literally (case sensitive)
\s matches any whitespace character (equal to [\r\n\t\f\v ])
\d{1,2} matches a digit (equal to [0-9]) between 1 and 2 times, as many times as possible, giving back as needed (greedy)
. matches the character . literally (case sensitive)
\d{1,2} matches a digit (equal to [0-9]) between 1 and 2 times, as many times as possible, giving back as needed (greedy)
. matches the character . literally (case sensitive)
\d{4} matches a digit (equal to [0-9]) exactly 4 times
\s matches any whitespace character (equal to [\r\n\t\f\v ])
\S matches any non-whitespace character (equal to [^\r\n\t\f\v ])
*? Quantifier — Matches between zero and unlimited times, as few times as possible, expanding as needed (lazy) , what specified in previous brackets
?= Positive Lookahead - Assert that the following Regex matches
\nDate Option 1
\n matches a line-feed (newline) character (ASCII 10)
Date matches the characters Date: literally (case sensitive)
$: Option 2 - $ asserts position at the end of the string, or before the line terminator right at the end of the string (if any)