Regex to replace all occurences of a character within a given context - regex

I am trying to write a search and replace regex (in ruby) to replace all instances of a character in a string in a given context.
The regex needs to replace all instances of "." in a json key, and I'm battling with references. I have a feeling that I need to use a lookaround in some way, but the variations I've tried I can't seem to get working.
Some example strings:
, "key1.name" : " value.something "
, "key2.complex.name" : "value.else"
, "this.is.the.most.complex.name" : "value"
I initially had this regex to replace a single occurrence (replacing it with "FULLSTOP"):
s/, "([^.]+)\.([^"]+)" :/, "\1FULLSTOP\2" :/gā€ā€
Desired output:
, "key1FULLSTOPname" : " value.something "
, "key2FULLSTOPcomplexFULLSTOPname" : "value.else"
, "thisFULLSTOPisFULLSTOPtheFULLSTOPmostFULLSTOPcomplexFULLSTOPname" : "value"
I'm guessing I need to use a (?=\.) somehow in the search, but not sure how to use this correctly with references. I am using the opening , and ending : as a way of defining the context for a json key.
thanks in advance.

(?=.*?\:)\.
Use this.See demo.
http://regex101.com/r/cH8vN2/5
Edit:
(?=.*?\"\s*\:)\.
Use this to be very sure.
See demo.
http://regex101.com/r/cH8vN2/6

You can use the following as a sample :
str = ', "this.is.the.most.complex.name" : "value';
str = str.gsub(/\.+/, 'FULLSTOP');
puts str;
I have not taken care of the 'value' part.
You should be able to do that easily.

Related

replace all with regex with spesify content

for simple example is when delete all console.log
its easy way use vscode and search console.log.*$ using regex.
but i have a string look like these
response.ok(res, "db_xx_model created", 1);
what i expected is can using regex with character (
look like response.ok(.*$ but i know these would invalid regex expression.
so how i can modified inside quota ( " " ) only.
response.ok(res, "i want modified these inside string only", 1);
other code maybe have more parameter so i want modif second parameter only .
response.ok(parameter1, "parameter2", 1);

Regular expression (extract key/value pairs)

I'm trying to extract a list (match) of key/value pairs from a string. Ex :
PATH_1:"/", PATH_2:"/OtherPath", TODAY:"2016-06-27",XYZ :"1234"
This should give :
Key Value
PATH_1 /
PATH_2 /OtherPath
TODAY 2016-06-27
XYZ 1234
Here is what I have so far as regex :
((?:"[^"]*"|[^:,])*):((?:"[^"]*"|[^:,])*)
This is well working except that when I'm adding a path having a '\'. Ex :
PATH_1:"c:\", PATH_2:"c:\OtherPath", TODAY:"2016-06-27"
I don't know how to instruct to regex expression to jump over semi-colon when found inside double quote sequence. Hope someone can help me.
PS : I'm using QT.
Best regards,
https://regex101.com/r/vB1rS1/2
It seems that just removing the : from the last [] may do it if the quotes are being removed.
((?:"[^"]*"|[^:,])*):((?:"[^"]*"|[^,])*)

Extract a between a specific pattern

I have to extract some substrings, this is like an XML markup in a plain text doc, like
lsdkfjsdklfj sdklfsdklfjsd <AAA>myString</AAA>sdfsdfsdfsdf
Can i extract this pattern in a single command?
In a case like this, I tried to use a matcher, the group command to extract this single match.
I don't want to do something like
String pattern = /<AAA>(.*)<\/AAA>/;
// Create a Pattern object
Pattern r = Pattern.compile(pattern);
// Now create matcher object.
Matcher m = r.matcher("lsdkfjsdklfj sdklfsdklfjsd <AAA>myString</AAA>sdfsdfsdfsdf");
if (m.find( )) {
System.out.println("Found value: " + m.group(0) );
}
There must be a more elegant way.
Edit :
Thank you time_yates, i was looking for something like that.
Could you explain a little why you use [0][1] on the result of
def extract = (input =~ '<AAA>(.+?)</AAA>')[0][1]
Answer by tim_yates :
=~ returns a Matcher, and so [0] gets the first match, which is 2 groups, the first is the String that had the match in it (your whole string) the second [1] is the group you defined in your expression
Thank you so much for your help, and thanks to all the readers.
Power of a community !!!
Can't you just do:
def input = 'lsdkfjsdklfj sdklfsdklfjsd <AAA>myString</AAA>sdfsdfsdfsdf'
def extract = (input =~ '<AAA>(.+?)</AAA>')[0][1]
assert extract == 'myString'
This is the shortest (not the best) way I can think of without external libs:
String str = "lsdkfjsdklfj sdklfsdklfjsd <AAA>myString</AAA>sdfsdfsdfsdf";
System.out.println(str.substring(str.indexOf(">") + 1, str.lastIndexOf("<")));
Or using StringUtils (which is million times better than my previous sugestion with substring):
StringUtils.substringBetween(str, "<AAA>", "</AAA>");
Still I'd go with matcher() like you proposed among all these.

Replace using regex containing some of previous match in Notepad++

I'm trying to use a regex to match a block of text, and using replace all, replace it with nothing, so as to delete it.
But Since I sometimes (but not always) have the block appear one after another when I try to replace all, it replaces every second block.
I made this Regex
http.*\n.*\K\n\{\n "code"(.*\n)+?\}\nhttp.*\n
But it will match all isolated blocks, but only every second consecutive block.
I think I'm meant to use "assertions" as described by here. But I couldn't get them to work.
Also how do I replace with nothing (as in delete)? Just leave an empty replace with field? or do I need some special character? Or as I am coming to suspect, I shouldn't use Notpad++ for this sort of thing? If that is the case what should/could I be using?
Sample Data:
"teamAbbr" : "Foo",
"teamName" : "Bar",
"teamNickname" : "FBar"
}
} ]
}
http://www.link_I_want_to_keep_belonging_to_above_data.com
{
"code" : "XXXXXXXXXXXXXXXXXXXXXXX",
"techMessage" : "XXXXXXXXXXXXXXXXXXXXXX",
"userMessage" : "XXXXXXXXXXXXXXXXXXX",
"host" : "XXXXXXXXXXXX",
"date" : "XXXXXXXXXXX",
"version" : "XXX"
}
http://www.url_that_belong_to_block_Iwant_to_be_rid_off.com
{
"code" : "XXXXXXXXXXXXXXXXXXXXXXX",
"techMessage" : "XXXXXXXXXXXXXXXXXXXXXX",
"userMessage" : "XXXXXXXXXXXXXXXXXXX",
"host" : "XXXXXXXXXXXX",
"date" : "XXXXXXXXXXX",
"version" : "XXX"
}
http://www.url_that_belong_to_block_Iwant_to_be_rid_off.com
The problem is that you also match the first url, but that is unavailable when immidiately after a match. And also at the start of the file.
Lookbehind assertions takes care of the problem, but needs to be fixed length.
Do you need to search for the first url? Ie. does
\{\n "code"(.*\n)+?\}\nhttp.*\n
work for you?
To delete a whole match you replace with an empty string. No special characters needed.

Perl: Help writing a regular expression

I am trying to write a common regular expression for the below 3 cases:
Supernatural_S07E23_720p_HDTV_X264-DIMENSION.mkv
the.listener.313.480p.hdtv.x264-2hd.mkv
How.I.met.your.mother.s02e07.hdtv.x264-xor.avi
Now my regular exoression should remove the series name from the original string i,e the output of above string will be:
S07E23_720p_HDTV_X264-DIMENSION.mkv
313.480p.hdtv.x264-2hd.mkv
s02e07.hdtv.x264-xor.avi
Now for the basic case of supernatural string I wrote the below regex and it worked fine but as soon as the series name got multiple words it fails.
$string =~ s/^(.*?)[\.\_\- ]//i; #delimiter can be (. - _ )
So, I have no idea how to proceed for the aboves cases I was thinking along the lines of \w+{1,6} but it also failed to do the required.
PS: Explanation of what the regular expression is doing will be appreciated.
you can detect if the .'s next token contains digit, if not, consider it as part of the name.
HOWEVER, I personally think there is no perfect solution for this. it'd still meet problem for something like:
24.313.480p.hdtv.x264-2hd.mkv // 24
Warehouse.13.s02e07.hdtv.x264-xor.avi // warehouse 13
As StanleyZ said, you'll always get into trouble with names containing numbers.
But, if you take these special cases appart, you can try :
#perl
$\=$/;
map {
if (/^([\w\.]+)[\.\_]([SE\d]+[\.\_].*)$/i) {
print "Match : Name='$1' Suffix='$2'";
} else {
print "Did not match $_";
}
}
qw!
Supernatural_S07E23_720p_HDTV_X264-DIMENSION.mkv
the.listener.313.480p.hdtv.x264-2hd.mkv
How.I.met.your.mother.s02e07.hdtv.x264-xor.avi
!;
which outputs :
Match : Name='Supernatural' Suffix='S07E23_720p_HDTV_X264-DIMENSION.mkv'
Match : Name='the.listener' Suffix='313.480p.hdtv.x264-2hd.mkv'
Match : Name='How.I.met.your.mother' Suffix='s02e07.hdtv.x264-xor.avi'
note : aren't you doing something illegal ? ;)