Sublime text regular expression search and replace - regex

Using Sublime Text 3, I have a text file with many lines like this:
{"Currency" : "Andorran Franc","Code" : "ADF","USD/1 Unit" : "0.1853","Units/1 USD" : "5.3967"}
{"Currency" : "Andorran Peseta","Code" : "ADP","USD/1 Unit" : "0.007306","Units/1 USD" : "136.8890"}
For each line I would like to eliminate the "Currency" field and its value so that the lines look like:
{"Code" : "ADF","USD/1 Unit" : "0.1853","Units/1 USD" : "5.3967"}
{"Code" : "ADP","USD/1 Unit" : "0.007306","Units/1 USD" : "136.8890"}
but I have having trouble coming up with a regular expression to apply.

Use the below regex and then replace the matched chars with an empty string.
"Currency"\s*:\s*"[^"]*",
OR
"Currency"\s*:.*?,(?="[^"]*"\s*:)
DEMO

"Currency"[^,]*,
Try this.Replace by empty string.See demo.
https://regex101.com/r/tX2bH4/13
If the format is fixed then it can be done in this simple way

Related

how to add special characters in mongo $regex

I want to look for "\r" in a string field I have in mongo, and I fount this, which looks like it works good:
db.users.findOne({"username" : {$regex : ".*son.*"}});
the problem is that i want to look for "\r" and I can find it, which I know its there, so I just did:
db.users.findOne({"username" : {$regex : ".*\r.*"}});
and it dosent work, how can I fix this?
example document:
{
"personId" : 1,
"personName" : "john",
"address" : {
"city" : "Rue Neuve 2\\r\\rue Pré-du-Mar \\r ché 1 1003 Lausanne",
"street" : "",
"zipCode" : "",
"streetNumber" : ""
}
}
so my query is:
db.users.findOne({"address.city" : {$regex : ".*\r.*"}});
also tried:
db.users.findOne({"address.city" : {$regex : ".*\\r.*"}});
try
db.users.findOne({"username" : {$regex : ".*\\r.*"}});
I think your issue is that you have your .* backwards at the end. You are looking for a "2." literal followed by any characters as opposed to what you have at the beginning, .*, saying anything before the literal that isn't a carriage return. Try to change this to
db.users.findOne({"username" : {$regex : ".*\\r*."}});
Which says give me "\r" with any non carriage return characters before the literal and any non carriage return characters after the literal.
I found that the way to do it is:
db.users.findOne({"username" : {$regex : ".*\\\\.*"}});

Regex to find string followed by character

I am a newbie in Regular expressions. What would be the regex to find all "DataKeyNames" followed by "," string? I am trying to find all text where we have "DataKeyNames" followed by "," in various files.
DataKeyNames="AppRevObjId, AsmtIdsOnHold"
i don't know what is you want to find but :
1) if you want 'AppRevObjId' only use this :
import re
s = 'AppRevObjId, AsmtIdsOnHold'
re.findall('([^\s]+),' , s)
output :
['AppRevObjId']
2) if you only want 'AsmtIdsOnHold' :
re.findall(',\s([^\s]+)' , s) # or-> re.findall(', ([^\s]+)' , s)
3) if you want both :
re.findall('([^\s]+), ([^\s]+)' , s) # or-> re.findall('([^\s]+),\s([^\s]+)' , s)
[('AppRevObjId', 'AsmtIdsOnHold')]
output:
[('AppRevObjId', 'AsmtIdsOnHold')]
so please explain more or report bugs to fix issues and bugs in codes

Regular Expression in Pig Latin

I want to search for the string '15200' (without quotes) in tuples. So, for the following input:
15200
15200,4000
4000,15200
4000,15200,4025
152000
152000,4000
4000,152000
4000,152000,4025
115200
115200,4000
4000,115200
4000,115200,4025
The output should be :
15200,15200
15200,4000,15200
4000,15200,15200
4000,15200,4025,15200
152000,-1
152000,4000,-1
4000,152000,-1
4000,152000,4025,-1
115200,-1
115200,4000,-1
4000,115200,-1
4000,115200,4025,-1
My Pig code looks like this:
A = LOAD '/user/test' USING PigStorage() AS (logic:chararray);
B = FOREACH A GENERATE
logic,
((logic matches '(^|,)15200($|,)')? '15200' :'-1') AS expt;
But when I Dump B, I get:
(15200,15200)
(15200,4000,-1)
(4000,15200,-1)
(4000,15200,4025,-1)
(152000,-1)
(152000,4000,-1)
(4000,152000,-1)
(4000,152000,4025,-1)
(115200,-1)
(115200,4000,-1)
(4000,115200,-1)
(4000,115200,4025,-1)
Try this:
.*?\b15200\b.*
Regex Demo: https://regex101.com/r/n6EP1s/2

Replace nth occurence of a character by another

I hope this isn't a duplicated, I didn't find an answer and I need help from regexp wizards.
I have a string and I would like to replace the second space found in it by a \n, but I don't know how to use indices (this way) in a regular expression :
For example :
# I have :
"a b c d e f"
# I want :
> "a b/nc d e f"
Also I would like to know how I can "repeat" this replacement: each two occurences of space replace by \n.
For example :
"a b c d e f"
> "a b\nc d\ne f"
(\\S+\\s+\\S+)\\s+
You can use this and replace by \1\n or $1\n.See demo.
https://regex101.com/r/yG7zB9/29

How to evaluate a word saved in a scalar via regular expression in Perl?

I would like to know if there's a way to save a word in a scalar and then use the scalar in a regex with symbols like "^", "$" in order to evaluate only that word and not a part of it.
This is my code :
#syn = qw(
abondamment,beaucoup,immensément
chose,objet,cause
objet,chose
ensemble,totalité,tout,unité
lit,coucher
ossimor);
#words = qw(
chose
os
totalité
lit
absolu);
foreach $w (#words){
for ($i = 0; $i < #syn; $i++){
if ( $syn[$i] =~ /$w/){
print "$w : $syn[$i]\n";
}
}
$w++;
}
The output is :
chose : chose,objet,cause chose : objet,chose os : chose,objet,cause
os : objet,chose os : ossimor totalité : ensemble,totalité,tout,unité
lit : ensemble,totalité,tout,unité lit : lit,coucher
As you can see "$w" matches, for ex., "chose" but also "os".
I'd like an output like that:
chose : chose,objet,cause chose : objet,chose totalité :
ensemble,totalité,tout,unité lit : lit,coucher
I've tried to write the regex as /^$w$/ but it doesn't work.
Any ideas?
Use this regex:
/\b$w\b/
Word boundaries http://perldoc.perl.org/perlre.html
From http://www.regular-expressions.info/wordboundaries.html :
There are three different positions that qualify as word boundaries:
- Before the first character in the string, if the first character is a word character.
- After the last character in the string, if the last character is a word character.
- Between two characters in the string, where one is a word character and the other is not a word character.