How to extract cells with text and different number sizes - regex

I am using VBA and I want to extract the following:
TEST-1, TEST-11, TEST-111 or TEST-1111 from a cell.
The regex that I've currently got is:
RE.Pattern = "(TEST-\d{3,4})"
This just extracts cells with either TEST-111 or TEST-1111. (It does not extract TEST-1 or TEST-11, which I also need) I have tried several different iterations or my regex with no luck.
Does anyone have any ideas of how to do this?

Looks like you need to use "(TEST-\d{1,4})" regexp.
This regex match all your exampless (see http://myregexp.com?regex=TEST-%5Cd%7B1,4%7D&text=TEST-1%20TEST-11%20TEST-111%20TEST-1111)

Related

Extract only the text field needed

I am at the beginning of learning Regex, and I use every opportunity to understand how it's working. Currently I am trying to extract dates from a text file (which is in fact a vnt-file type from my mobile phone). It looks like following:
BEGIN:VNOTE
VERSION:1.1
BODY;ENCODING=QUOTED-PRINTABLE;CHARSET=UTF-8:18.07.=0A14.08.=0A15.09.=0A15.10.=
=0A13.11.=0A13.12.=0A12.01.=0A03.02. Grippe=0A06.03.=0A04.04.2015=0A0=
5.05.2015=0A03.06.2015=0A03.07.2015=0A02.08.2015=0A30.08.2015=0A28.09=
17.11.2017=0A
DCREATED:20171118T095601
X-IRMC-LUID:150
END:VNOTE
I want to extract all dates, so that the final list is like that:
18.07.
14.08.
15.09.
15.10.
and so on. If the date has also a year, it should also be displayed.
I almost found out how to detect the dates by the following regex:
.+(\d\d\.\d\d\.(2015|2016|2017)?).+
But it only detect very few of the dates. The result is this:
BEGIN:VNOTE
VERSION:1.1
15.10.
04.04.2015
30.08.2015
24.01.2016
DCREATED:20171118T075601
X-IRMC-LUID:150
END:VNOTE
Then I tried to add a question mark which makes the .+ not greedy, as far as I read in tutorials. Then the regex looks like:
.+?(\d\d\.\d\d\.(2015|2016|2017)?).+?
But the result is still not what I am looking for:
BEGIN:VNOTE
VERSION:1.1
21.03.20.04.18.05.18.06.18.07.14.08.15.09.15.10.
13.11.13.12.12.01.03.02.06.03.04.04.20150A0=
03.06.201503.07.201502.08.201530.08.20150A28.09=
28.10.201525.11.201528.12.201524.01.20160A
DCREATED:20171118T075601
X-IRMC-LUID:150
END:VNOTE
For someone who is familiar with regex I am pretty sure this is very easy to solve, but I don't get it. It's very confusing when you are new to regex. I tried to find a hint in some tutorials or stackoverflow posts, but all I found is this: Notepad++ how to extract only the text field which is needed?
But it doesn't work for me. I assume it might have something to do with the fact that my text file is not one single line.
I have my example on regex101 too.
I would be very thankful if maybe someone can give me a hint what else I can try.
Edit: I would like to detect the dates with the regex and as a result have a list with only the dates (maybe it is called substitute?)
Edit 2: Sorry for not mentioning it earlier: I just want to use the regex in e.g. Notepad++ or an online regex test website. Just to get the result of the dates and save the result in a new txt-file. I don't want to use the regex in an programming language. My apologies for not being precisely before.
Edit 3: The result should be a list with the dates, and each date in a new line:
I want to extract all dates, so that the final list is like that:
18.07.
14.08.
15.09.
15.10.
I suggest this pattern:
(?:.*?|\G)(\d\d\.\d\d\.(?:\d{4})?)
This makes use of the \G flag that, in this case, allows for multiple matches from the very start of the match without letting any single unmatched character in the text, thus allowing the removal of all but what's wanted.
If you want to remove the extra matches as well, add |.* at the end:
(?:.*?|\G)(\d\d\.\d\d\.(?:\d{4})?)|.*
regex101 demo
In N++, make sure the options underlined are selected, and that the cursor is at the beginning. In the picture below, I replaced then undid the replacement, only to show that matches were identified (16 replacements).
You can try using the following pattern:
\d{2}\.\d{2}\.(?:\d{4})?
This will match day.month dates of the form 18.07., but it also allows such a date to be followed by a four digit year, e.g. 18.07.2017. While it would be nice to make the pattern more restrictive, to avoid false fire matches, I do not see anything obvious which can be added to the above pattern. Follow the demo link below to see the pattern in action.
Demo

Regex to highlight Strings in code

I'm developing a small code editor and I would like to match Strings so I can highlight them a different color.
Example:
myvar = array('VOLVO', 'TOYOTA')
Using regex expression \'.*\' I get one match 'VOLVO', 'TOYOTA'
However, what I want are two matches: 'VOLVO' and 'TOYOTA'
Is this possible to achieve with a single regex expression?
Someone did suggest the following expression:
\'[^\']*\'
Which in fact solves my problem: I get two matches, one for 'VOLVO' and one for 'TOYOTA'.

Simple find-and replace regexp

In a .txt file i have multiple lines. Every line contains timing data like this:
time [4.1s] [4100ms]
time [5.53s] [5530ms]
All lines have different words/chars before and after the times.
I want to do a Find- and replace action (In Notepad++) to get the following, simple, format:
4.1
5.53
How do I do it? What is the regular expression to use?
Any help is greatly appreciated!
Find:
.*\[([\d.]+)s\].*
Replace with:
\1
Assuming that you only want the first number in brackets and that has a decimal point as per your example:
\d*[.]\d+
This returns 4.1 and 5.53 as requested when applied to your example.
If the first number might not have a decimal point, then you want to consider:
\d*[.]?\d+s
but append s in your replace to account for the s.
Update
Update based on your latest information. I don't know if Notepad++ supports positive lookbehind (?<=), but if it does you could do this:
(?<=time \[)\d*[.]\d+

Regular Expression Replace String

I have a rather complicating data file with many rows of many different types. For the particular column I'm interested in I have a pattern that looks like this:
12.6 \pm 0.8
^^ The number of digits before and after the decimal in each of those pieces of the entry may vary.
I'm hoping I can use regular expressions to replace that column entry to:
[12.6,-0.8,+0.8]
What I am requesting help on is how I should go about replacing once I've found entries like what I had earlier. All of the examples I've found so far are for when you want to replace static strings with other static strings, but for each line I'm necessarily going to have different numbers (and different digits perhaps). The regular expression I've attempted so far to find entries like "12.6 \pm 0.8" is the following:
\d*\.\d*\s\\\w{2})\s\d*\.\d*
I would also appreciate if I could get a check on that, too. At the moment I'm just manipulating the datafile in my text editor, but I'm also open to Python solutions, too.
Thanks!
Your expression is close. Are there any conditions where this won't work?
(\d*\.\d*)\s\\\w{2}\s(\d*\.\d*)
with the replace pattern being (for JS)
[$1, -$2, $2]
or for emacs (according to http://www.emacswiki.org/emacs/RegularExpression)
[\1, -\2, \2]

How to insert before and after a pattern in notepad++ with regular expression?

I have a big txt file ,with many strings ,say "string-I-want-to-change",now I need to change all of them into this :
string-insert-before-string-I-want-to-change-string-insert-after
I mean ,find a pattern ,insert something before and after the pattern ,but just keep the pattern.I tried many ways ,they just replaced the pattern with the string I want to insert.
I tried the method here
Using RegEX To Prefix And Append In Notepad++
and here
regular expression to add characters before and after numbers
But they seem do not work with my version of notepad++,I am using the newest version 6.1.2
Some one please help me :)
Thank you!
This would work for you.
find : (string-i-want-to-change)
replace : string-in-front-of-it-\1-string-after-it
test string :
other-strings-came-in-front-of-it-string-i-want-to-change-and-it-continues-like-that
output :
other-strings-came-in-front-of-it-string-in-front-of-it-string-i-want-to-change-string-after-it-and-it-continues-like-that