textmate regex date search - regex

I'm using text mate to search for date strings in the format 30/08/2016 and tried adding this to the regex find panel: ^\d{1,2}\/\d{1,2}\/\d{4}$
but it yields no results

You are using beginning of the string ^ and end of the string $. Therefore, it only works, if you have a sample string like this: 12/12/1212 but not like this: abc12/12/1212def. You also have to escape a forward slash /.
If you don't care about this string being inside the text, you can use
\d{1,2}\/\d{1,2}\/\d{4}
If you know, that there is white space around it, you can use this:
(?<=\s)(\d{1,2}\/\d{1,2}\/\d{4})(?=\s)
Or the simpler solution with word boundaries \b
\b\d{1,2}\/\d{1,2}\/\d{4}\b
Don't forget to use the global flag, if you are trying to match multiple instance of this date pattern.

Related

Regex search for a fixed string that could have non-alphanumeric characters inside the string

I am trying to find a specific string (invoice number) like: VFE033586 inside a digitized invoice (raw text), but some customers use hyphens inside those invoice numbers like: VFE-033586.
Is there a possibility to create a regex expression that can use the original string without modifying it, so, it finds even if there is a hyphen in the raw text invoice? Like ignoring non-alphanumeric character.
I've tried to search, but did not come up with anything similar.
Thanks.
Try this one:
[A-Z]+-?[0-9]+
or more general with groups: ([A-Z]+)[\W]?([\d]+)
Example: https://regex101.com/r/oelSH0/2

Regex Find Spaces between single qoutes and replace with underscore

I have a database table that I have exported. I need to replace the image file name with a space and would like to use notepad++ and regex to do so. I have:
'data/green tea powder.jpg'
'data/prod_img/lumina herbal shampoo.JPG'
'data/ALL GREEN HERBS.jpeg'
'data/prod_img/PSORIASIS KIT (640x530) (2).jpg'
and need to make them look like this:
'data/green_tea_powder.jpg'
'data/prod_img/lumina_herbal_shampoo.JPG'
'data/ALL_GREEN_HERBS.jpeg'
'data/prod_img/PSORIASIS_KIT_(640x530)_(2).jpg'
I just want to change the spaces between the quotes (I don't want to change the capitalization). To be more specific I would like to replace any and all spaces between 'data/ and ' because there are other spaces between quotes in the DB, for example:
'data/ REPLACE ANY SPACE HERE '
I found this:
\s(?!(?:[^']*'[^']*')*[^']*$)
but there are other places where there are spaces between quotes so I'd like to search for data/ in the beging and not just a single quote but I can't figure out how. I tried \s(?!(?:[^'data\/]*'[^']*')*[^']*$) but it didn't work and I am not familiar enough with regex to make it do so.
An example of a full line from the database is:
(712, 'GRTE-P', '', 'data/green tea powder.jpg', '2014-03-12 22:52:03'),
I don't want to replace the spaces in the time and data stamp at the end of the line, just the image file names.
Thanks in advance for your help!
You have to use a \G based pattern to ensure that matches are contiguous.
search: (?:\G(?!^)|'data/)[^' ]*\K[ ]replace: _
The first match uses the second branch of the alternation, then the next matches are contiguous and use the first branch.

preserve all group in regexp

I have question regarding regexp, I have text like this
embedded-software-entwickler
basically I want to replace the - with something else but preserving the group so I can easily do $1#$2#$3 with # as replacement of -
my current regexp is like this ([a-zäöüß]+)(-) but this one will not hit the third word which is entiwckler
How about something simple like this:
([\w]*?)-([\w]*?)-([\w]*)
Replace with:
$1#$2#$3
What we did here is basically we started looking for any available character using \w and using the lazy sign *? at the beginning and the greedy sign * at the end to match each group, and separated each section with -.
If you would like to include spaces, numbers, special characters, etc. in each section, you can use something like this:
([\s\S]*?)-([\s\S]*?)-([\s\S]*)
If you prefer something dynamic, you could try something like this:
([^\-]+)-
Replace with:
$1#
Demo: https://regex101.com/r/p6zQTO/1/
Alternative way to mach each group plus the replacement:
([^\-]*)-([^\-]*)
Replace with:
$1#$2
Demo: https://regex101.com/r/p6zQTO/2/
If your need is simply to change all '-' into '#', trying a tr/-/#/m would produce simpler and better substitution.
If you need to group and extract for other purposes, then try something like /(\w+)(?:-(\w+))*/
(?:groups but don't extract)

VIM - Replace based on a search regex

I've got a file with several (1000+) records like :
lbc3.*'
ssa2.*'
lie1.*'
sld0.*'
ssdasd.*'
I can find them all by :
/s[w|l].*[0-9].*$
What i want to do is to replace the final part of each pattern found with \.*'
I can't do :%s//s[w|l].*[0-9].*$/\\\\\.\*' because it'll replace all the string, and what i need is only replace the end of it from
.'
to
\.'
So the file output is llike :
lbc3\\.*'
ssa2\\.*'
lie1\\.*'
sld0\\.*'
ssdasd\\.*'
Thanks.
In general, the solution is to use a capture. Put \(...\) around the part of the regex that matches what you want to keep, and use \1 to include whatever matched that part of the regex in the replacement string:
s/\(s[w|l].*[0-9].*\)\.\*'$/\1\\.*'/
Since you're really just inserting a backslash between two strings that you aren't changing, you could use a second set of parens and \2 for the second one:
s/\(s[w|l].*[0-9].*\)\(\.\*'\)$/\1\\\2/
Alternatively, you could use \zs and \ze to delimit just the part of the string you want to replace:
s/s[w|l].*p0-9].*\zs\ze\*\'$/\\/

RegEx to match string between delimiters or at the beginning or end

I am processing a CSV file and want to search and replace strings as long as it is an exact match in the column. For example:
xxx,Apple,Green Apple,xxx,xxx
Apple,xxx,xxx,Apple,xxx
xxx,xxx,Fruit/Apple,xxx,Apple
I want to replace 'Apple' if it is the EXACT value in the column (if it is contained in text within another column, I do not want to replace). I cannot see how to do this with a single expression (maybe not possible?).
The desired output is:
xxx,GRAPE,Green Apple,xxx,xxx
GRAPE,xxx,xxx,GRAPE,xxx
xxx,xxx,Fruit/Apple,xxx,GRAPE
So the expression I want is: match the beginning of input OR a comma, followed by desired string, followed by a comma OR the end of input.
You cannot put ^ or $ in character classes, so I tried \A and \Z but that didn't work.
([\A,])Apple([\Z,])
This didn't work, sadly. Can I do this with one regular expression? Seems like this would be a common enough problem.
It will depend on your language, but if the one you use supports lookarounds, then you would use something like this:
(?<=,|^)Apple(?=,|$)
Replace with GRAPE.
Otherwise, you will have to put back the commas:
(^|,)Apple(,|$)
Or
(\A|,)Apple(,|\Z)
And replace with:
\1GRAPE\2
Or
$1GRAPE$2
Depending on what's supported.
The above are raw regex (and replacement) strings. Escape as necessary.
Note: The disadvatage with the latter solution is that it will not work on strings like:
xxx,Apple,Apple,xxx,xxx
Since the comma after the first Apple got consumed. You'd have to call the regex replacement at most twice if you have such cases.
Oh, and I forgot to mention, you can have some 'hybrids' since some language have different levels of support for lookbehinds (in all the below ^ and \A, $ and \Z, \1 and $1 are interchangeable, just so I don't make it longer than it already is):
(?:(?<=,)|(?<=^))Apple(?=,|$)
For those where lookbehinds cannot be of variable width, replace with GRAPE.
(^|,)Apple(?=,|$)
And the above one for where lookaheads are supported but not lookbehinds. Replace with \1Apple.
This does as you wish:
Find what: (^|,)(?:Apple)(,|$)
Replace with: $1GRAPE$2
This works on regex101, in all flavors.
http://regex101.com/r/iP6dZ8
I wanted to share my original work-around (before the other answers), though it feels like more of a hack.
I simply prepend and append a comma on the string before doing the simpler:
/,Apple,/,GRAPE,/g
then cut off the first and last character.
PHP looks like:
$line = substr(preg_replace($search, $replace, ','.$line.','), 1, -1);
This still suffers from the problem of consecutive columns (e.g. ",Apple,Apple,").