Regex in Notepad++ to add something after first character - regex

So I have example lines like these:
JJmartin
It needs to become:
J.Jmartin
I need regex to be able to insert . after first character in each line
i tried ^. and replace with . but that regex delete first character and replaces it with .
I also had an idea of maybe deleting everything after first character and then putting it together again with a program i have since i have a regex that deletes everything but the last character, so i tried to tweek it, but didn't work, that regex is:
.*([A-Za-z\d]) replace with \1

Find ^(.{1}) That is, from the beginning of each line, capture a single character.
Replace \1\. That is, the captured character with the same character followed by a dot. The dot should be escaped because of its meaning within regex.

Related

Removing last character from a line using regex

I just started learning regex and I'm trying to understand how it possible to do the following:
If I have:
helmut_rankl:20Suzuki12
helmut1195:wasserfall1974
helmut1951:roller11
Get:
helmut_rankl:20Suzuki1
helmut1195:wasserfall197
helmut1951:roller1
I tried using .$ which actually match the last character of a string, but it doesn't match letters and numbers.
How do I get these results from the input?
You could match the whole line, and assert a single char to the right if you want to match at least a single character.
.+(?=.)
Regex demo
If you also want to match empty strings:
.*(?=.)
This will do what you want with regex's match function.
^(.*).$
Broken down:
^ matches the start of the string
( and ) denote a capturing group. The matches which fall within it are returned.
.* matches everything, as much as it can.
The final . matches any single character (i.e. the last character of the line)
$ matches the end of the line/input

How to write a regexp that matches from a character to end of line?

I want a regexp to clear (for each line) anything that comes after # sign. So I wrote the code like this:
contents.replace(QRegExp("#.*\n"),"")
However, since the . also matches the newline, this cleared everything after the first #. So how can I correctly write the regexp?
You need a negated character class [^\n]
contents.replace(QRegExp("#[^\n]*"),"")
This will remove # and the rest of the line.
See the regex demo at regex101.com (it will work the same in Qt).
In case you need to also remove the newline after, add it to the pattern:
contents.replace(QRegExp("#[^\n]*\n"),"")

Understanding regex in shell

I came across single grouping concept in shell script.
cat employee.txt
101,John Doe,CEO
I was practising SED substitute command and came across with below example.
sed 's/\([^,]*\).*/\1/g' employee.txt
It was given that above expression matches the string up to the 1st comma.
I am unable to understand how this matches the 1st comma.
Below is my understanding
s - substitute command
/ delimiter
\ escape character for (
( opening braces for grouping
^ beginning of the line - anchor
[^,] - i am confused in this , is it negate of comma or mean something else?
why * and again .* is used to match the string up to 1st comma?
^ matches beginning of line outside of a character class []. At the beginning of a character class, it means negation.
So, it says: non-comma ([^,]) repeated zero or more times (*) followed by anything (.*). The matching part of the string is replaced by the part before the comma, so it removes everything from the first comma onward.
I know 'link only' answers are to be avoided - Choroba has correctly pointed out that this is:
non-comma ([^,]) repeated zero or more times () followed by anything (.). The matching part of the string is replaced by the part before the comma, so it removes everything from the first comma onward.
However I'd like to add that for this sort of thing, I find regulex quite a useful tool for visualising what's going on with a regular expression.
The image representation of your regular expression is:
Given the string "foo, bar", s/\([^,]*\).*/\1/g, and more specifically \([^,]\)*) means, "match any character that is not a comma" (zero or more times). Since "f" is not a comma, it matches "f" and "remembers" it. Because it is "zero or more times", it tries again. The next character is not a comma either (it is o), then, the regex engine adds that o to the group as well. The same thing happens for the 2nd o.
The next character is indeed a comma, but [^,] forbids it, as #choroba affirmed. What is in the group now is "foo". Then, the regex uses .* outside the group which causes zero or more characters to be matched but not remembered.
In the replacement part of the regex, \1 is used to place the contents of the remembered text ("foo"). The rest of the matched text is lost and that is how you remain with only the text up to the first comma.

regular expression to match everything until the last occurrence of /

Using a regular expression (replaceregexp in Ant) how can I match (and then replace) everything from the start of a line, up to and including the last occurrence of a slash?
What I need is to start with any of these:
../../replace_this/keep_this
../replace_this/replace_this/Keep_this
/../../replace_this/replace_this/Keep_this
and turn them into this:
what_I_addedKeep_this
It seems like it should be simple but I'm not getting it. I've made regular expressions that will identify the last slash and match from there to the end of the line, but what I need is one that will match everything from the start of a line until the last slash, so I can replace it all.
This is for an Ant build file that's reading a bunch of .txt files and transforming any links it finds in them. I just want to use replaceregexp, not variables or properties. If possible.
You can match this:
.*\/
and replace with your text.
DEMO
What you want to do is match greedily, the longest possible match of the pattern, it is default usually, but match till the last instance of '/'.
That would be something like this:
.*\/
Explanation:
. any character
* any and all characters after that (greedy)
\/ the slash escaped, this will stop at the **last** instance of '/'
You can see it in action here: http://regex101.com/r/pI4lR5
Option 1
Search: ^.*/
Replace: Empty string
Because the * quantifier is greedy, ^.*/ will match from the start of the line to the very last slash. So you can directly replace that with an empty string, and you are left with your desired text.
Option 2
Search: ^.*/(.*)
Replace: Group 1 (typically, the syntax would be $1 or \1, not sure about Ant)
Again, ^.*/ matches to the last slash. You then capture the end of the line to Group 1 with (.*), and replace the whole match with Group 1.
In my view, there's no reason to choose this option, but it's good to understand it.

How do I remove everything beyond the second occurrence of a character using Regex?

I have a list that is something like:
xxxxx|xxxxxx|xxxxx#example.com
xxxxx|xxxxxx|xxxxx#example.com
xxxxx|xxxxxx|xxxxx#example.com
The x's can be both letters and numbers, but nothing else.
What I am trying to do is replace everything (including the character) to the right of the second |. The outcome should look like:
xxxxx|xxxxx
I want to do it with regex so that I can replace all the occurrences in Notepad++.
Find
^([^|]*|[^|]*)|.*$
Replace with
\1
Reference: http://www.scintilla.org/SciTERegEx.html
Replace \|[^\|]+$ with nothing.
This will match |xxxxx#example.com, so if you replace it with a blank string, that will accomplish what you want.