I need to return only non-repeating elements from a string.
This is my current regex:
/{{([1-9]|[1-9][0-9])}}/gm
Here are some samples of what I need:
{{1}}{{2}}{{3}}{{4}}{{5}}{{1}} => {{1}}{{2}}{{3}}{{4}}{{5}}
{{1}}{{2}}{{3}}{{4}}{{2}}{{1}} => {{1}}{{2}}{{3}}{{4}}
{{1}}{{2}}{{2}}{{4}}{{1}}{{1}} => {{1}}{{2}}{{4}}
I have tried the following lookaround:
/{{([1-9]|[1-9][0-9])}}(?!.*\1)/gm
Kind of works for this example, but it get the last element instead of the first:
{{1}}{{2}}{{3}}{{4}}{{5}}{{1}} => {{2}}{{3}}{{4}}{{5}}{{1}}
With this example it don't work:
Olá, {{1}}
Só pra lembrar, sua consulta foi agendada para o dia {{2}} às {{3}}h.
Por favor, não se atrase
Se por acaso tiver algum imprevisto e não puder comparecer, nos avisem com {{4}}h de antecedência.
{{1}}, te esperamos aqui!
The string above returns all elements:
{{1}}{{2}}{{3}}{{4}}{{1}}
If I understand problem correctly, here is what you need:
You want to match {{<num>}} patterns
But don't want to match duplicates occurrence of same pattern
Duplicates of {{<num>}} can occur any where in multiline text
You prefer to match first occurrence of duplicates
Since you showed Javascript syntax of your regex, I am also using lookbehind feature in my regex as per modern Javascript behavior:
({{(?:[1-9]|[1-9]\d)}})(?<!^(?:[^]*\1){2})
RegEx Demo 1
RegEx Demo 2
RegEx Breakup:
({{(?:[1-9]|[1-9]\d)}}): Match {{<num>}} text and capture in group #1
(?<!^(?:[^]*\1){2}): Negative lookbehind with a dynamic length to assert failure if we have 2 repeats of capture group #1
I have to modify a SQL file with vi to delete columns that we do not use. As we have a lot of data, I use the search and replace option with a Regex Pattern.
For instance we have :
(1,2956,2026442,4,NULL,NULL,'ZAC DU BOIS DES COMMUNES','',NULL,NULL,'Rue DU LUXEMBOURG',NULL,
'9999','EVREUX',NULL,1,'27229',NULL,NULL,NULL,NULL,NULL,' Rue DU LUXEMBOURG, 9999 EVREUX',NULL,NULL,NULL,NULL,
NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,'2020-07-08 16:34:40',NULL,NULL)
So we have 40 columns and I keep 13 ones. My regex is :
(1),2,(3),4-5,(6-14),15-22,(23),24-39,(40)
:%s/(\(.\{-}\),.\{-},\(.\{-}\),.\{-},.\{-},\(.\{-},.\{-},.\{-},.\{-},.\{-},.\{-},.\{-},.\{-},.\{-}\),.\{-},
.\{-}, .\{-},.\{-},.\{-},.\{-},.\{-},.\{-},\(.\{-}\),.\{-},.\{-},.\{-},.\{-},.\{-},.\{-},.\{-},.\{-},.\{-},.\{-},.\{-},
.\{-},.\{-},.\{-},.\{-},.\{-},\(.\{-}\))/(\1,\2,\3,\4,\5)/g
I enclose in my parenthesis the parts that interest me by putting them in parenthesis (I only get the values in parenthesis on the line above my regex ). Then with the replace I recover these groups.
So normally my result is suppose to be :
(1,2026442,NULL,'ZAC DU BOIS DES COMMUNES','',NULL,NULL,'Rue DU LUXEMBOURG',NULL,
'9999','EVREUX',' Rue DU LUXEMBOURG, 9999 EVREUX',NULL)
But Because in ' Rue DU LUXEMBOURG, 9999 EVREUX' there is a comma (,). My result become :
(1,2026442,NULL,'ZAC DU BOIS DES COMMUNES','',NULL,NULL,'Rue DU LUXEMBOURG',NULL,'9999','EVREUX',' Rue DU LUXEMBOURG',NULL,NULL)
Does Someone who is good in Regex can help me ? thanks in advance. If I wasn't clear tell me too, i will try to explain better next time.
I suggest matching fields that can be strings with a %('[^']*'|\w*) pattern, that is, a non-capturing group that finds either ' + zero or more non-'s and then a ' char, or any zero or more alphanumeric characters.
Also, the use of non-capturing groups (in Vim, it is %(...) in very magic mode, or \%(...\) in a regular mode) and very magic mode can help shorten the pattern.
The whole pattern will look like
:%s/\v\(([^,]*),[^,]*,([^,]*),[^,]*,[^,]*,(%('[^']*'|\w*)%(,%('[^']*'|\w*)){8})%(,%('[^']*'|\w*)){8},('[^']*'|\w*)%(,%('[^']*'|\w*)){16},([^,]*)\)/(\1,\2,\3,\4,\5)/g
See the regex demo converted to a PCRE regex.
Note some fields that are not strings are matched with [^,]* that matches zero or more chars other than a comma. The %(,%('[^']*'|\w*)){8} like patterns match (here) 8 occurrences of a sequence of a , char + '...' substring or zero or more word chars.
I'm trying to remove all spaces around quotes with one Ruby regex. (not the same question as this)
Input: l' avant ou l 'après ou encore ' maintenant'
Output: l'avant ou l'après ou encore 'maintenant'
What I tried:
(/'\s|\s'/, '')
It's matching a few cases, but not all.
How to perform this ? Thanks.
TLDR:
I assume the spaces were inserted by some automation software and there can only be single spaces around the words.
s = "l' avant ou l 'apres ou encore ' maintenant' ou bien 'ceci ' et ' encore de l ' huile ' d 'accord d' accord d ' accord Je n' en ai pas .... s ' entendre Je m'appelle Victor"
first_rx = /(?<=\b[b-df-hj-np-tv-z]) ' ?(?=\p{L})|(?<=\b[b-df-hj-np-tv-z]) ?' (?=\p{L})/i
# If you find it overmatches, replace [b-df-hj-np-tv-z] with [dlnsmtc],
# i.e. first letters of word that are usually contracted
second_rx = /\b'\b\K|' *((?:\b'\b|[^'])+)(?<=\S) *'/
puts s.gsub(first_rx, "'")
.gsub(second_rx) { $~[1] ? "'#{$~[1]}'" : "" }
Output:
l'avant ou l'apres ou encore 'maintenant' ou bien 'ceci' et 'encore de l'huile' d'accord d'accord d'accord Je n'en ai pas .... s'entendre Je m'appelle Victor
Explanation
The problem is really complex. There are several words that can be abbreviated and used with an apostrophe in French, de, le/la, ne, se, me, te, ce to name a few, but these are all consonants. You may remove all spaces between a single, standalone consonant, apostrophe and the next word using
s.gsub(/(?<=\b[b-df-hj-np-tv-z]) ' ?(?=\p{L})|(?<=\b[b-df-hj-np-tv-z]) ?' (?=\p{L})/i, "'")
If you find it overmatches, replace [b-df-hj-np-tv-z] with [dlnsmtc], i.e. first letters of word that are usually contracted. See the regex demo.
Next step is to remove spaces after initial and before trailing apostrophes. This is tricky:
s.gsub(/\b'\b\K|' *((?:\b'\b|[^'])+)(?<=\S) *'/) { $~[1] ? "'#{$~[1]}'" : "" }
where \b'\b is meant to match all apsotrophes in between word chars, those that we fixed at the previous step. See this regex demo. As there is no (*SKIP)(*F) support in Onigmo regex, the regex is a bit simplified but the replacement is a conditional one: if Group 1 matched, replace with ' + Group 1 value ($1) + ', else, replace with an empty string (since \K reset the match, dropped all text from the match memory buffer).
NOTE: this approach can be extended to handle some specific cases like aujourd'hui, too.
To remove all whitespace around the ', use gsub!, applied in several steps for proper whitespace removal:
str = "l' avant ou l 'apres ou encore ' maintenant'"
str.gsub!(/\b'\s+\b/, "'").gsub!(/\b\s+'\b/, "'").gsub!(/\b(\s+')\s+\b/, '\1')
puts str
# l'avant ou l'apres ou encore 'maintenant'
Here,
\b : word boundary,
\s+ : 1 or more whitespace,
string.gsub!(regex, replacement_string) : replace in the string argument regex with specified replacement_string (during this, the original string is changed),
\1 : in the replacement string, this refers to the first group captured in parenthesis in the regex: (...).
So if you have alot of data like this, all the answers I have seen are wrong, and will not work. No regex can guess wether the preceding word should have a space or not. Unless you came up with a list of words (or patterns) that either do or don't.
The problem is, sometimes a space should be left, sometimes not. The only way to script that is to find a pattern which describes when the space should be there, or when not. You must teach your regex French grammar. It may be possible lol. But probably not, or difficult.
If this is a one off, my advice is to create regexes for 2 or 3 different situations, and use something like vim, to go through the data, and select manually yes or no to substitute each occurrence.
There may be some cases you can run - eg remove all spaces to the right of quotes? - but unfortunately I don't think you can automate this process.
I believe the following should work for you
s.gsub(/'.*?'/){ |e| "'#{e[1...-1].strip}'" }
The regex portion lazy matches all text within single quotes (including quotes). Then, for each match you substitute for the quoted text with leading and trailing whitespace removed, and return this text in quotes.
I'm trying to match this expression:
^COMA1TA2000,.*$
with this text:
# Qualquer linha iniciada por # será ignorada
# Caracteres que não podem serem usados na nomenclatura das copiadoras ou modelos.
# & < > " '
COMA1TA2000,ta-2000,hd,COMB1
#COMA2TA2000,ta-2000,hd,COMB2
#COMA3TA2000,ta-2000,hd,COMB3
I can do that using Notepad++, but I can't with the C# Regex class.
content = sr.ReadToEnd();
string pattern = "^COMA1TA2000,.*$";
if(Regex.IsMatch(content, pattern))
System.Windows.Forms.MessageBox.Show("Test");
What am I missing?
You can use RegexOptions.Multiline, like so:
Regex.IsMatch(content, pattern, RegexOptions.Multiline)
Docs: http://msdn.microsoft.com/en-us/library/system.text.regularexpressions.regexoptions.aspx
If RegexOptions.Multiline is not set, ^ and $ will match beginning and the end* of the string, not the line like intended.
Or set multiline option in regex (?m): (?m)^COMA1TA2000,.*$
Use File.ReadLines instead of ReadToEnd method and apply regex on each line.
What you're doing now, is reading the entire text as a block of text and MultiLine option sometimes breaks things down (as there are different CRLF characters in different operating systems, and stuff like that).
My suggestion is this:
string[] lines = File.ReadAllLines("path to your text file");
Regex regex = new Regex("^pattern$");
foreach (string line in lines)
{
Match match = regex.Match(line.Trim())
if (match.Successful)
{
// have your match here.
}
}
I have a large Property file where I need to quickly remove all the SPACE from the keys which got added by mistake.
I am using Sublime Text. Where I need to use regex to match the following.
What I have:
Rio de Janeiro=Рио де Жанейро
Rio Grande do Norte=Рио Гранде до Норте
Rio Grande do Sul=Рио Гранде направя Sul
What I Need:
RiodeJaneiro=Рио де Жанейро
RioGrandedoNorte=Рио Гранде до Норте
RioGrandedoSul=Рио Гранде направя Sul
I have gone through the Regex list and tried to come up with my own but failed. I need a quick solution so I am here.
Any help is much appreciated. Thanks.
Hit CTRL + H
Find What: [ ]+(?=.*?=)
Replace With:
Live Demo
Explanation:
Search for:
[ ]+(?=[A-Za-z].*=)
And replace with nothing.
Explain:
[ ]+ : 1 or more spaces
(?=[A-Za-z].*=) : a positive lookahead
that asserts the space is followed by a letter ([A-Za-z]),
then other characters `.*` and a `=` character.