This question already has answers here:
Regular Expression, remove everything after last forward slash
(5 answers)
Closed 3 years ago.
I am trying to get the syntax right so that I can make scanning-client-container-0.2.tar look like scanning-client-container
I am using the delimiter " - " like so:
sed -e 's/-[^*]*$//'
with the result scanning, which is cut off too early
You can use a negated character class in your regex:
sed 's/-[^-]*$//' <<< 'scanning-client-container-0.2.tar'
scanning-client-container
RegEx Details:
-: Match a -
[^-]*: Match 0 or more characters that are not -
$: Match end
Related
This question already has answers here:
What is the difference between .*? and .* regular expressions?
(3 answers)
What do 'lazy' and 'greedy' mean in the context of regular expressions?
(13 answers)
Closed 5 months ago.
There is a line of text:
Lorem ~Ipsum~ is simply ~dummy~ text ~of~ the printing...
To find all the words enclosed in ~~ I use
re.search(r'~([^~]*)~', text)
Let's say it became necessary to use ~~ instead of ~
([^\~]+) indicates to exclude the ~ character from the text within those characters
How do I make a regular expression to exclude a string of characters instead of just one?
That is, ~~Lor~em~~ should return Lor~em
The symbol of the new string must not be excluded and the length of the found string cannot be 0
Use a non-greedy quantifier instead of a negated character set.
re.search(r'~~(.*?)~~', text, flags=re.DOTALL)
re.DOTALL makes . match newline characters.
This question already has answers here:
How to use sed/grep to extract text between two words?
(14 answers)
Closed last year.
The community reviewed whether to reopen this question 10 months ago and left it closed:
Original close reason(s) were not resolved
I would like to keep the strings between (FROM and as), and (From and newline character).
Input:
FROM some_registry as registry1
FROM another_registry
Output:
some_registry
another_registry
Using the following sed command, I can extract the strings. Is there a way to combine the two sed commands?
sed -e 's/.*FROM \(.*\) as.*/\1/' | sed s/"FROM "//
Merging into one regex expression is hard here because POSIX regex does not support lazy quantifiers.
With GNU sed, you can pass the command as
sed 's/.*FROM \(.*\) as.*/\1/;s/FROM //' file
See this online demo.
However, if you have a GNU grep you can use a bit more precise expression:
#!/bin/bash
s='FROM some_registry as registry1
From another_registry'
grep -oP '(?i)\bFROM\s+\K.*?(?=\s+as\b|$)' <<< "$s"
See the online demo. Details:
(?i) - case insensitive matching ON
\b - a word boundary
FROM - a word
\s+ - one or more whitespaces
\K - "forget" all text matched so far
.*? - any zero or more chars other than line break chars as few as possible
(?=\s+as\b|$) - a positive lookahead that matches a location immediately followed with one or more whitespaces and then a whole word as, or end of string.
This question already has answers here:
Notepad++ and regex: how to UPPERCASE specific part of a string / find / replace
(2 answers)
Closed 4 years ago.
In notepad++ I need to use RegEx transform all
phone1_id, phone2_id, phone3_id
in
PHONE1_ID, PHONE2_ID, PHONE3_ID
This RegEx helps me find all those strings: phone\d+_id
but how can I transform them to capital case?
Ctrl+H
Find what: phone\d+_id
Replace with: \U$0
check Wrap around
check Regular expression
Replace all
Replacement:
\U : Change to uppercase
$0 : contains the whole match
Result for given example:
PHONE1_ID, PHONE2_ID, PHONE3_ID
This question already has answers here:
Notepad++ Regex - Issue with ^ anchor and repeating patterns
(2 answers)
Closed 5 years ago.
To remove, e.g. (exactly) 2 leading spaces from each line, I've tried to replace
"^ "
with
""
I tried that with our own text editor and with Notepad++. Both behave the same and start the search at the same position where the last found/replace happend, so it will actually remove 2n spaces from each line (n >= 0). Is this the expected behavior? Is my used regular expression wrong for that task or do our own text editor and Notepad++ behave incorrectly?
The issue here is that Notepad++ will keep replacing a pattern so long as it keeps finding matches. This means that replacing ^ will keep stripping whitespace from the start of the string, so long as there are two or more leading spaces available.
Try this as a workaround:
Find:
^ (.*)$
Replace:
$1
This question already has answers here:
How to check if a string contains a substring in Bash
(29 answers)
Closed 4 years ago.
In a bash shell, I want to take the take a given string that matches a regex, and then take the part of the string.
For example, given https://github.com/PatrickConway/repo-name.git, I want to extract the repo-name substring.
How would I go about doing this? Should I do this all in a shell script, or is there another way to approach this?
You can use the =~ matching operator inside a [[ ... ]] condition:
#!/bin/bash
url=https://github.com/PatrickConway/repo-name.git
if [[ $url =~ ([^/]*)\.git ]] ; then
echo "${BASH_REMATCH[1]}"
fi
Each part enclosed in parentheses creates a capture group, the corresponding matching substring can be found in the same position in the BASH_REMATCH array.
[...] defines a character class
[/] matches a character class consisting of a single character, a slash
^ negates a character class, [^/] matches anything but a slash
* means "zero or more times"
\. matches a dot, as . without a backslash matches any character
So, it reads: remember a substring of non-slashes, followed by a dot and "git".
Or maybe a simple parameter expansion:
#!/bin/bash
url=https://github.com/PatrickConway/repo-name.git
url_without_extension=${url%.git}
name=${url_without_extension##*/}
echo $name
% removes from the right, # removes from the left, doubling the symbol makes the matching greedy, i.e. wildcards try to match as much as possible.
Here's a bashy way of doing it:
var="https://github.com/PatrickConway/repo-name.git"
basevar=${var##*/}
echo ${basevar%.*}
...which gives repo-name