Replacing two characters with two different characters using regex in python - regex

I want to replace two different characters with two other different characters using regex in python in one operation. For example: The word is "a/c stuff" and i want to transform this into "ac_stuff" using regex in one regex.sub() line.
I searched here, but find ways to solve this using replace function, but i am looking to do this using regex in one line.
Thank you for the help!

Technically possible, but not pretty to do this in one line using re.sub
re.sub("[/ ]", (lambda match: '' if match.group(0) == '/' else '_'), "a/c stuff")
Much nicer (and faster) way using str.translate
"a/c stuff".translate(str.maketrans({'/': None, ' ': '_'}))
or
"a/c stuff".translate(str.maketrans(' ', '_', '/'))
Probably the most readable way is through str.replace, though this doesn't scale well to many replacements.
"a/c stuff".replace('/', '').replace(' ', '_')

Related

Functions on regexp_replace result in Postgres

I have a database of addresses where acronyms have been separated with a space, I want to remove these space so I turned to trusty regular expressions. However, I am struggling to perform a secondary function on the regexp result '\&' - I have checked the forums and docs and just cannot get this to work. Example data I have is as follows:
'A V C Welding' should be 'AVC Welding'
'H S B C' should be 'HSBC'
etc..
I have the following regexp:
trim(regexp_replace(organisation || ' ', '(([A-Z]\s){1}){2,}', replace('\&',' ',''), 'g'))
The replace('\&',' ','') is not having any effect at all, I just get the same string back. I have tried other functions e.g. lower('\&') and none of these seem to work as expected. Concatenation with || does work however. I have tried casting the '\&' to text, tried replace('' || '\&' || '',' ','') - still, no joy.
Any advice would be much appreciated, I am sure the solution is something very simple but I just cannot see where to go next!
What you are trying to do with \& will never work. The \& pattern will replace the entire pattern, but you need a solution that works on individual parts.
What you need is to replace the pattern CAPITAL-space with just CAPITAL but only when followed by another capital which is not the start of a longer word. In other words: you need a negative lookahead and if the pattern is matched, then replace only the first atom of the replace string:
select regexp_replace('A V C Welding', '([A-Z]){1}(\s){1}(?![A-Z][a-z])', '\1', 'g');
You can replace the negative lookahead pattern with something broader if needed (such as no capital letter start, numbers, punctuation, etc.).

Matching multiple quoted strings in a single line with regex

I want to match quoted strings of the form 'a string' within a line. My issue comes with the fact that I may have multiple strings like this in a single line. Something like
result = functionCall('Hello', 5, 'World')
I can search for phrases bounded by strings with ['].*['], and that picks up quoted strings just fine if there is a single one in a line. But with the above example it would find 'Hello', ', 5, ' and 'World', when I only actually want 'Hello' and 'World'. Obviously I need some way of knowing how many ' precede the currently found ' and not try to match when there is an odd amount.
Just to note, in my case strings are only defined using ', never ".
you should use [^']+ between quotes:
var myString = "result = functionCall('Hello', 5, 'World')";
var parts = myString.match(/'[^']+'/g);

Avoid repeating regex substitution

I have lines of code (making up a Ruby hash) with the form of:
"some text with spaces" => "some other text",
I wrote the following vim style regex pattern to achieve my goal, which is to replace any spaces in the string to the left of the => with +:
:%s/\(.*\".*\)\ (.*\"\ =>.*\,)/\1+\2
Expected output:
"some+text+with+spaces" => "some other text",
Unfortunately, this only replaces the space nearest to the =>. Is there another pattern that will replace all the spaces in one run?
Rather than write a large complex regex a couple of smaller ones would easier
:%s/".\{-}"/\=substitute(submatch(0), ' ', '+', 'g')
For instance this would capture the everything in quotes (escaped quotes break it) and then replace all spaces inside that matched string with pluses.
If you want it to work with strings escaped quotes in the string you just need to replace ".\{-}" with a slightly more complex regex "\(\\.\|[^\"]\)*"
:%s/"\(\\.\|[^\"]\)*"/\=substitute(submatch(0), ' ', '+', 'g')
If you want to restrict the lines that this substitute runs on use a global command.
:g/=>/s/"\(\\.\|[^\"]\)*"/\=substitute(submatch(0), ' ', '+', 'g')
So this will only run on lines with =>.
Relevant help topic :h sub-replace-expression
It's really far from perfect, but it does nearly the job:
:%s/\s\ze[^"]*"\s*=>\s*".*"/+/g
But it doesn't handle escape quotes, so the following line won't be replaced correctly:
"some \"big text\" with many spaces" => "some other text",

How to search for all uppercase words in vim?

I would like to search for all uppercase words in a file but I have no idea how to do it (or if it's possible). I found this solution here on stackoverflow, but it doesn't work on vim.
From command mode, assuming you do not have the option ignorecase set:
/\<[A-Z]\+\>
or
/\v<[A-Z]+>
Finds any string of capital letters greater than length one surrounded by word boundaries. The second form uses 'very-magic'. :help magic for details
The shortest answer: /\<\u\+\>
If you want a list of all the matching uppercase words (i.e. you aren't interested in jumping from one word to the other), you can use:
echo filter(split(join(getline(1, '$'), ' '), '\v(\s|[[:punct:]])'), 'v:val =~ "\\v<\\u+>"')
With:
getline(1, '$') that returns a list of all the lines from the current buffer
join(lines, ' ') that flattens this list of lines
split(all_text, separators_regex) that build a list of word-like elements
and finally filter(words, uppercase-condition) that selects only the uppercase words.

Regex pattern to match where my code breaks

I have the following values that I want to place into a mysql db
The pattern should look as follows, I need a regex to make sure that the pattern is always as follows:
('', '', '', '', '')
In some rare execution of my code, I hower get the following output where one of the apostrophes disapear. it dissapears every now and then on the 4th record. like in the code below where I placed the *
('1', '2576', '1', '*, 'y')
anyideas to solve this will be welcomed!
This should be able to match one of the times the code breaks
string.replace(/, \',/ig, ', \'\',');
how would I do it if it is like this
('1', '2576', '1', 'where I have text here and it breaks at the end*, 'y')
I am using javascript and asp
I think the solution would be something like this
string.replace(/, \'[a-zA-Z0-9],/ig, ', \'\','); but not exactly sure how to write it
This is almost the solution that I am looking for...
string.replace(/[a-zA-Z0-9], \'/ig, '\', \'');
this code however replaces the last letter of the text with the ', ' so if the text inside the string is 'approved, ' it will replace the 'approve', ' and cut off the letter d
I know there is a way that you can reference it not to remove the last letter but not sure how to do it
Is this what you're looking for? It matches when all but the last field is missing the '
\('.*?'\)
Your regular expression, would be something like this:
^\('.*?',\ '.*?',\ '.*?',\ '.*?',\ '.*?'\)$
you could check if your string matchs in ASP.net with some code similar to this:
Match m = Regex.Match(inputString, #"^\('.*?',\ '.*?',\ '.*?',\ '.*?',\ '.*?'\)$");
if (!m.Success)
{
//some fix logic here
}