Regex to match the last character if it is a dot - regex

In OpenOffice Calc how do I remove the last character if it is a dot?
Example:
dog
cat.
rabbit
It becomes:
dog
cat
rabbit
What regex would you use to obtain that?

Regular expression:
To match a ., you have to write \., because . has a special meaning in regular expressions.
To match the end of the line, you have to write $.
Putting it all together, I would use this regular expression: \.$. This will match a dot if it's at the end of the line. Demo
Removing the matched dots:
Open "Find & Replace" window by pressing Ctrl+H
Type regular expression from above into "Search for" field, this is what you want to be replaced
Leave "Replace with" field empty, as you want to replace with nothing, which means it will remove the matched content (the dots)
Under "More/Other options", turn on "Regular expressions"*
Click on "Replace" or "Replace all" button to perform the replacements
Please see documentation of this Calc feature for further details.

In OpenOffice Calc how do I remove the last character if it is a dot?
=IF(RIGHT(A1)=".";LEFT(A1;LEN(A1)-1);A1)

Related

Regular expression to replace 2023-02-06T07:43:51.9732381Z with blanks

I have a date stamp at the beginning of my log files, in the format 2023-02-06T07:43:51.9732381Z
There are other various dates, I have tried to use notepad ++ to write a regular expression to replace all the data formats with just blanks. I would like to have a regular expression to replace the dates.
Secondly, what if I wanted the dates to be in the format 2023-02-06T07:43:51 ?
To remove the leading date on every line do this:
Find what: ^2023[^ ]* (with trailing space)
Replace with: (empty string)
check the "Regular expression" radio button
To remove just the fractional seconds on every line do this:
Find what: ^(2023[^\.]*)[^ ]*
Replace with: $1
check the "Regular expression" radio button
The manual indicates
that Notepad++ uses "Boost" for regular expressions with its search syntax and replacement syntax.
So:
2023-02-06T07:43:51.9732381Z
could be matched at the start of lines by:
^\d{4}-\d\d-\d\dT\d\d:\d\d:\d\d[.]\d{7}Z
or more loosely by:
^\d+-\d+-\d+T\d+:\d+:\d+[.]\d+Z
To retain the part before the period, "mark" the sub-expression you want to keep:
^(\d+-\d+-\d+T\d+:\d+:\d+)[.]\d+Z
and refer to it in the replacement:
$1

How to replace uppercase letters by their lowercase counterparts?

I have a big text document in which suspension points are followed by a capital letter. It should be lowercase. Now I figured to use this expression in search & replace: …\ [:upper:] which works fine to find the parts I want to replace, but when I try to do that with …\ [:lower:] I pastes literally that expression rather than the same letter but lowercase. What am I doing wrong? Thanks!
You can't use [:lower:] this way, because it's only a pattern to match the search text; it doesn't affect or transform the matched text part.
To solve your issue:
Put the search pattern in round brackets. This makes the current matched text available for the replacement pattern.
Reference the current match in the replacement pattern using $1 (assuming there's only one pair of round brackets in your search pattern);
Tell LO Writer to use lower-case characters when replacing.
Step by step (the following example will simply replace every upper-case letter by its lower-case counterpart):
Open find/replace (CTRL+H or Menu Edit -> Find/Replace...)
As search pattern, enter ([:upper:])
Make sure that "Regular expressions" is selected in "Other Options";
As replace pattern, enter $1 (this simply uses the complete current match as replacement);
Still with cursor in the "Replace" input box, hit the Format... button; this will open the "Replace with Formatting" window.
In the "Replace with Formatting" window, select "Font effects", and from "Effects" -> "Case", select "lowercase". Hit OK.
Execute the Find/Replace.

RegEx in Notepad++ to find and replace strings

I'm trying to find and replace strings using RegEx on Notepad++, but cannot identify the right expression to do it:
Here is the data:
TRAIN-II
TRAIN
TRAIN-I
AIRPLANE-II
AIRPLANE
AIRPLANE-I
SHIP-II
SHIP
SHIP-I
Well, I want to keep only the string which has "-II" as suffix. In simpler words, I want to retain only:
TRAIN-II
AIRPLANE-II
SHIP-II
Can anyone please help?
Ctrl+H
Find what: ^.*(?<!-II)(?:\R|\Z)
Replace with: LEAVE EMPTY
CHECK Wrap around
CHECK Regular expression
UNCHECK . matches newline
Replace all
Explanation:
^ # beginning of line
.* # 0 or more any character but newline
(?<!-II) # negative lookbehind, make sure we haven't -II before the following
(?:\R|\Z) # non capture group, any kind of linebreak (i.e. \r, \n, \r\n) or end of file
Screenshot (before):
Screenshot (after):
Use a regex to mark the lines, then remove unmarked lines.
Open the search window and then the "Mark" tab.
Enter -II$ into the "Find what" box. Select "Regular expressions". Select "Match case" if the "II" must be in upper case. Select "Bookmark line". Click on "Mark all". Expect to see the wanted lines marked with a blue circle. Use menu => Search => Bookmark => Remove unmarked lines.

Notepad++ Search and Replace: delete all after "/" in each row

I have a plain text file with content like this:
prežrať/RN
prežrieť/Z
prežrúc/zZ
prežuť/c
...
Q: How can I remove all strings after / symbol in every row in Notepad++?
Desired output:
prežrať
prežrieť
prežrúc
prežuť
...
I am doing this with Find-and-Replace for every different string after /, but there are too many combinations.
Search for: /.*, replace with nothing.
The character / matches just /. ., however, matches any character except newlines, so .* will match a sequence of characters up until the first newline. You can find a demonstration here: http://regex101.com/r/kT0uE3.
If you want to remove characters only after the last on the line /, you should use the regex /[^/]*$. You can find an explanation and demonstration here: https://regex101.com/r/sZ6kP7/74.
In regular expression mode
Find:
/.*
Replace:
(empty)
Set find and replace to regular expression mode.
Find string: /.*
Replace String: (empty string)
Notepad++ find and replace is by default line ended (it won't go over multiple lines)
Using find and replace:
Hit CTRL-H to open the Replace dialogue box
enter /.* into "Find what"
leave "Replace with" empty
Select "Regular expression" (and .matches newline if it is single line)
Click on Replace
Here we go... You are done.

Notepad++ regex -> newLine

I use Notepad++ and I need to delete all lines starting with, say "abc".
Attention, I don't need to replace the line starting with "abc" with a empty line, but I need to completely delete these lines.
How do I proceed (using regex, I suppose)?
Try replace
^abc.*(\r?\n)?
with
nothing
The ^ indicates the start of a line.
The . means wild-card.
The .* means zero or more wild-cards.
x? means x is optional.
The \r?\n covers both \r\n (generally Windows) and \n (generally Unix), but must be optional to cover the last line.
Search for this regular expression
^abc.*\r\n
Replace with nothing.
Searching a little bit more on regex in Notepad++ I discovered that the new line character is not \n as I expected (Windows), but the \n\r.
So, my regex replace expression should be:
Find: abc.*\r\n
Replace with: (nothing, empty field)
Try the regex \nabc.* in "Find and Replace" --> "Replace"
Leave "Replace With" field empty.
EDIT : This won't work with first like (because '\n' means "new line")
Press Ctrl+H to bring up the Replace window. Put
^abc.*(\r?\n)?
in the Find what and leave Replace with empty. Select Reqular expression and hit Replace All.
This reqular expression handles all the edge cases:
When the first line of the file starts with abc
When the last line of the file starts with abc and there is no new line at the end of the file.