Regex pattern to match where my code breaks - regex

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
}

Related

SQLite Pattern Matching with Extra Character

My database contains these rows:
DuPage
Saint John
What queries could I use that would match people entering either 'Du Page' or 'SaintJohn': in other words: adding an extra character (at any position) that shouldn't be there, or removing a character (at any position) that should be there?
The first example has a possible workaround: I could just remove the space character from the 'Du Page' input before searching the table, but I cannot do that with the second example unless there was some way of saying 'match 'SaintJohn' with the database text that has had all spaces removed', or alternatively 'match a database row that has every letter in 'SaintJohn' somewhere in the row.
Remove spaces from the column and the search text:
select * from tablename
where replace(textcolumn, ' ', '') like '%' || replace('<your search string>', ' ', '') || '%'

Replacing two characters with two different characters using regex in python

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(' ', '_')

Regex: how to separate strings by apostrophes in certain cases only

I am looking to capitalize the first letter of words in a string. I've managed to put together something by reading examples on here. However, I'm trying to get any names that start with O' to separate into 2 strings so that each gets capitalized. I have this so far:
\b([^\W_\d](?!')[^\s-]*) *
which omits selecting the X' from any string X'XYZ. That works for capitalizing the part after the ', but doesn't capitalize the X'. Further more, i'm becomes i'M since it's not specific to O'. To state the goal:
o'malley should go to O'Malley
o'malley's should go to O'Malley's
don't should go to Don't
i'll should go to I'll
(as an aside, I want to omit any strings that start with numbers, like 23F, that seems to work with what I have)
How to make it specific to the strings that start with O'? Thx
if you use the following pattern:
([oO])'([\w']+)|([\w']+)
then you can access each word by calling:
match[0] == 'o' || match[1] == 'name' #if word is "o'name"
match[2] == 'word' #if word is "word"
if it is one of the two above, the others will be blank, ie if word == "word" then
match[0] == match[1] == ""
since there is no o' prefix.
Test Example:
>>> import re
>>> string = "o'malley don't i'm hello world"
>>> match = re.findall(r"([oO])'([\w']+)|([\w']+)",string)
>>> match
[('o', 'malley', ''), ('', '', "don't"), ('', '', "i'm"), ('', '', 'hello'), ('', '', 'world')]
NOTE: This is for python. This MIGHT not work for all engines.

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",