This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 6 years ago.
I'm not familiar with regex, but I have the following string
d.$filter = d.$filter.replace(/TutoringSince le '(.+?)'/g, "TutoringSince ge $1Z");
Is anybody know what does it mean this Z char after $1 ?
$1 is a reference to the first group - in this case: (.+?)
Z is just a Z letter.
It replaces the captured text in the d.$filter, with TutoringSince ge, plus itself then adds a Z. I.e. the text
TutoringSince le 'whatever'
would turn into
TutoringSince ge whateverZ
"The captured text" would be anything inside the single quotes.
Related
This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 2 years ago.
I have this string like "682_2, 682_3, 682_4". (682 is a random number)
How can i get this string "2, 3, 4" using regex and ruby?
You can do this in ruby
input="682_2, 682_3, 682_4"
output = input.gsub(/\d+_/,"")
puts output
A simple regex could be
/_([0-9]+)$/ and in the match group of the result you will have 2 for 682_2 and 3 for 682_3
Ruby code snippet would be "64532_2".match(/_([0-9]+)/).captures[0]
you can use scan which returns an array containing the matches:
string_code.scan(/(?<=_)\d/)
(?<=_) tells to find a pattern that has a given pattern (_ in this case) before itself but wont capture that, it captures only \d. if it can have more than 1 digit like 682_13,682_33 then \d+ is necessary.
This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 5 years ago.
I went over a piece of code in which a subroutine takes video filename as argument, and then printing it's duration time. Here I'm only showing the snippet.
sub videoInfo {
my $file = shift;
$file =~ s/(\W)/\\$1/g;
}
So far I understood is that it is dealing with whitespaces but I'm not able to break the meaning of code, I mean what is $1 and how it will work?
It puts backslashes in front of non-word characters. Things like "untitled file" becomes "untitled\ file".
As in most regular expression operations $1 represents the first thing captured with (...) which in this case is the (\W) representing a single non-word character.
I think this is an unnecessary home-rolled version of quotemeta.
This question already has answers here:
Reference - What does this regex mean?
(1 answer)
Can I use named groups in a Perl regex to get the results in a hash?
(5 answers)
Closed 5 years ago.
I found this regular expression in Raisin code:
my ($a, $b, $r) = ("(?<$token>", ')', undef);
In this case $token is some word (param name). This code used in functon for creating regular expression. Sample result for /users/:id is:
/users/(?<id>[^/]+?)(?:\.[^.]+?)?
I dont understand what does its mean: (?<id> ?
I know positive and negative look-behind - (?<= and (?<!. But i don`t know this.
This question already has answers here:
Get string between two strings in a string
(27 answers)
Closed 6 years ago.
I've to detect and extract from a string a repeating group of characters and list one part of each captured group.
Here is an example of string to parse: "za e eStartGood1Endds qStartGood2Endsds df"
My Regex is: ".*?(?::Start(.+)End.*?)+"
Expecting groups captured expected: Good1, Good2, etc
My Regex capture is wrong: it seems that (?::Start(.+) is considered as group to capture...
May I miss something?
Thanks!
This regex do the job :
/(?<=Start)(.+?)(?=End)/g
Why not use this pattern: \*{2}Start\*{2}(.*?)\*{2}End\*{2}
For this input string: za e e**Start**Good1**End**ds dq**Start**Good2**End**sds df, it captures Good1 and Good2.
You can play with it here: https://regex101.com/r/dG0dX6/2
This question already has answers here:
Regex to pick characters outside of pair of quotes
(6 answers)
Closed 7 years ago.
I want to fetch the word outside a quotation for example if I have this text:
xxxx xxx OK xxxxx "xxx OK xxxx"
I want to fetch the "OK" outside the quotation which mean first one
I make this regular expression
[ ]OK[ ]
but it fetch the both. So how can I fetch only outside a quotation with regular expression?
You may use the combination of caturing group and the regex alternation operator.
"[^"]*"|( OK )
And now the group index 1 contain the <space>OR<space> which exists outside the double quoted part.
Code:
Matcher m = Pattern.compile("\"[^\"]*\"|( OK )").matcher(s);
while(m.find())
{
System.out.println(m.group(1));
}