Regex: how to find a comma between two quotation marks - regex

I need to find the dot between two quotation marks, and substitute it with a comma.
I'm trying with this
\".*?\"
but it finds everything between the quotation marks.
I need to transform something like this "100,21$" into this "100.21$"

In general, you can match quoted substrings first with a
"[^"]+"
and then replace the . with , in the matched block (in a callback, or a post-process method/function).
Alternatively, you might use capturing + backreferences:
"(\d*),(\d+\$)"
to replace with "$1.$2" (where $1 is the text captured with (\d*) and $2 is the value captured with (\d+\$)). See this demo.

If your string is simple like your exemple : "100,21$"
You can simple use this code :
str = str.replace(/,/g,'.');
If it's a little bit more complex like :
Here is an "example" of "100,21$" price, I am a string
You can use this :
str = str.replace(/"(\d+),(\d+)(.*?)"/g,'"$1.$2$3")
Regexr link if you want to test :
http://regexr.com/3dbo5
nb : I wrote the example, but you can use the regexp and the string pattern in another langages

Related

Regex to match text between single, double and triple quotes

I have a text file that I want to parse strings from. The thing is that there are strings enclosed in either single ('), double (") or 3x single (''') quotes within the exact same file. The best result I was able to get so far is to use this:
((?<=["])(.*?)(?=["]))|((?<=['])(.*?)(?=[']))
to match only single-line strings between single and double quotes. Please note that the strings in the file are enclosed in each type of quotes can be either single- or multi-line and that each type of string repeats several times within the file.
Here's a sample string:
<thisisthefirststring
'''- This is the first line of text
- This is the second line of text
- This is the third line of text
'''
>
<thisisanotheroption
"Just a string between quotes"
>
<thisisalsopossible
'Single quotes
Multiple lines.
With blank lines in between
'
>
<lineBreaksDoubleQoutes
"This is the first sentence here
After the first sentence, comes the blank line, and then the second one."
>
Use this:
((?:'|"){1,3})([^'"]+)\1
Test it online
Using the group reference \1, you can simplify your work
Also, to get only what is inside of the quotes, use the 2nd group of the match
This regex: ('{3}|["']{1})([^'"][\s\S]+?)\1
does what you want.
Some results:
Using Notepad++, you can use: ('''|'|")((?:(?!\1).)+)\1
Explanation:
('''|'|") : group 1, all types of quote
( : group 2
(?:(?!\1).)+ : any thing that is not the quote in group 1
) : end group 2
\1 : back reference to group 1 (i.e. same quote as the beginning)
Here is a screen capture of the result.
Here's something that may work for you.
^(\"([^\"\n\\]|\\[abfnrtv?\"'\\0-7]|\\x[0-9a-fA-F])*\"|'([^'\n\\]|\\[abfnrtv?\"'\\0-7]|\\x[0-9a-fA-F])*'|\"\"\"((?!\"\"\")[^\\]|\\[abfnrtv?\"'\\0-7]|\\x[0-9a-fA-F])*\"\"\")$
Replace the triple double quotes with triple single quotes. See it in action at regex101.com.
Named Group Version
Avoids problems when used in larger expressions by explicitly referring to the name of the group storing the last found quote.
Should work for most systems:
(?<Qt>'''|'|")(.*?)\k<Qt>
.NET version:
(?<Qt>'''|'|"")(.*?)\k<Qt>
Works as follows:
'''|'|": Check first for ''', then ', and finally ". Done in this order so ''' has priority over '.
(?<Qt>'''|'|""): When matched, place the match in <Qt> for later use.
(.*?): Capture the results of a lazy search for 0 or more of anything .*? - will return empty strings. To prevent empty strings from being returned, change to a lazy search for 1 or more of anything .+?.
\k<Qt>: Search for the value last stored in <Qt>.

regex to duplicate a word and add in extra text

I have a long list of words that I want to duplicate
Example
CallDateTime
WebDateTime
WavName
Dnis
Verified
Concern
ConcernCode
I'm trying to understand some regex to copy each word and placing to the right, along with adding in some needed text
's/(\t+)_(\w+)/\u\2, \u\1, \0/'
Well.. that is not working , THIS IS EXPECTED OUTPUT NEEDED
#CallDateTime = i.CallDateTime,
#WebDateTime = i.WebDateTime,
etc...
Obviously adding in ^ with # is easy and $ with , , but I want to also copy with a regex
I have seen this
((\w+)_(\w+))
Replace Pattern:
\3, \2, \1
But I don't understand that ..
Let's solve this with notepad++:
Find what: (\w+)
Replace with: #\1 = i.\1,
Explanation:
\w+ matches one or more word characters
(...) is a capturing group. You can reference it with \1 in the replacement part
replacement: A literal #, then the captured word, then a space, etc...
Searching for .+ and replacing it with #$0 = i.$0, should do the job.
https://regex101.com/r/WQXFy6/3
Replace :
\b(\w+)\b
by
#\1 = i.\1
Javascript code :
var str = "CallDateTime\nWebDateTime\nWavName\nDnis\nVerified\nConcern\nConcernCode";
str = str.replace(/\b(\w+)\b/g, '#$1 = i.$1');
console.log(str);

Removing quotation marks around text in IntelliJ (with Regex?)

I have a piece of code where I want to remove the quotation marks around property names.
// Current format
var user = {
'name': 'A',
'loggedIn': true
}
// Desired format
var user = {
name: 'A',
loggedIn: true
}
I've managed to find all the places I wish to change with this regular expression:
'(.+)'\:
Now I want to remove the quotation marks in those strings. I tried to enter (.+)\: into the "replace with" field, but it did not work. Is there some way to do what I want to do with this tool?
Find in Path documentation explains how to use the references:
if you specify the search pattern through a regular expression, use the $n format in back references (to refer to a previously found and saved pattern).
$1 will contain whatever is matched by the parenthesis, so your replacement string would look like $1:.
See also Regular Expression Syntax Reference.
Your regex matches with your desire strings, but you missed using captured groups! $1 returns first group and second and 3rd comes with $2 and $3 and ... .
Additional words:
You can back-referencing by \1 in your find regex to avoid repeating capture groups codes.
I suggest use this regex instead of your own in general cases:
^\s*(['|"])(.*?)\1\s?:
and replace by $2: to extract string between '/".

Regex to match word that is not in double or single quotes

I am trying to write regex code that matches a word that is not in quotes. I used a look ahead and look behind to try and accomplish but it doesnt seem to be working. Here is my code
(?<!"")echo(?>!"")
I want it to match only when echo is not in single or double quotes i.e
match:
echo "i need help";
and not:
$str = " you can use echo to print out words";
by the way am using vb.net 2012
The way is to use capture groups and to match content inside quote before example:
("[^"]*"|'[^']*')|\becho\b
When the capture group 1 is empty, so you have matched "echo".
Note: the description of the content between quotes can be improved to handle escaped quotes.
Note2: if you project to parse PHP code, you need to handle Heredoc and Nowdoc syntaxes too since you can define a string with them.
So the full pattern becomes (ignoreWhiteSpaces option):
(?=['"<])
(
# Heredoc/Nowdoc syntaxes:
<<<(['"]?)(\w+)\2 \r?\n # header
(?>.*\r?\n)*? # content
\3;?\r?\n # close the string
|
# quoted strings with escaped quotes support
"(?s)(?>[^\\"]+|\\.)*"
|
'(?s)(?>[^\\']+|\\.)*'
)
|
\becho\b
demo
The advantage of this way is that it stay fast whatever the size of the string and it's deals with escaped quotes in strings.
You can see the number of steps needed to obtain a match with regex101 debugger.

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\*\'$/\\/