I have get a set of Strings with pattern like:
category=50025969&city=%CE%C2%D6%DD&auction_start_seg=-1
now I wish to extract all substrings like:
city=%CE%C2%D6%DD
How can I write a regex to express that?
there's a lot of possible regex to answer your question. Here is mine:
(?=city=)[^&]*
gives you city=VALUE where match starts in city= until it finds a & char.
Related
I've tried a lot to split this string into something i can work with, however my experience isn't enough to reach the goal. Tried first 3 pages on google, which helped but still didn't give me an idea how to properly do this:
I have a string which looks like this:
My Dogs,213,220#Gallery,635,210#Screenshot,219,530#Good Morning,412,408#
The result should be:
MyDogs
213,229
Gallery
635,210
Screenshot
219,530
Good Morning
412,408
Anyone have an idea how to use regex to split the string like shown above?
Given the shared patterns, it seems you're looking for a regex like the following:
[A-Za-z ]+|\d+,\d+
It matches two patterns:
[A-Za-z ]+: any combination of letters and spaces
\d+,\d+: any combination of digits + a comma + any combination of digits
Check the demo here.
If you want a more strict regex, you can include the previous pattern between a lookbehind and a lookahead, so that you're sure that every match is preceeded by either a comma, a # or a start/end of string character.
(?<=^|,|#)([A-Za-z ]+|\d+,\d+)(?=,|#|$)
Check the demo here.
I have an example string:
*DataFromAdHoc(cbgv)
I would like to extract by RegEx:
DataFromAdHoc
So far I have figured something like that:
^[^#][^\(]+
But Unfortunately without positive result. Do you have maybe any idea why it's not working?
The regex you tried ^[^#][^\(]+ would match:
From the beginning of the string, it should not be a # ^[^#]
Then match until you encounter a parenthesis (I think you don't have to escape the parenthesis in a character class) [^\(]+
So this would match *DataFromAdHoc, including the *, because it is not a #.
What you could do, it capture this part [^\(]+ in a group like ([^(]+)
Then your regex would look like:
^[^#]([^(]+)
And the DataFromAdHoc would be in group 1.
Use ^\*(\w+)\(\w+\)$
It just gets everything between the * and the stuff in brackets.
Your answer may depend on which language you're running your regex in, please include that in your question.
I want to get the index of all tags inside brackets using regex package.
str := "[tag=blue]Hello [tag2=red,tag3=blue]Good"
rg := regexp.MustCompile(`(?:^|\W)\[([\w-]+)=([\w-]+)\]`)
rgi := fmtRegex.FindAllStringIndex(str, -1)
fmt.Println(rgi)
// Want index for:
// [tag=blue], [tag2=red,tag3=blue]
The regex needs to return indexes for [tag=blue], [tag2=red,tag3=blue]
but it only returns [tag=blue].
How do I fix this regex (?:^|\W)\[([\w-]+)=([\w-]+)\] so that I can also match the comman when there is more than one tags in the brackets
I would like to post a comment to the Answer by #Avinash Raj but I don't have enought Repotation... so:
Seems like you want something like this,
...
\B\[([\w-]+)=([\w-]+)(?:,[\w-]+=[\w-]+)*\]
The provided regular expression will match only the first and the last pair of key=value in the string. Having something like:
[tag=val,tag1=val1,tag2=val2,tag3=val3]
The regular expression will only match the tag, val, tag3 and val3.
If you want to match all of them I would suggest using pure go without regular expressions. This is something that should be almost straight forward in go.
If you actually need only the index for the match, you can use the above regular expression and then parse the tags some other way.
Seems like you want something like this,
(?<!\w)\[([\w-]+)=([\w-]+)(?:,[\w-]+=[\w-]+)*\]
DEMO
OR
\B\[([\w-]+)=([\w-]+)(?:,[\w-]+=[\w-]+)*\]
\B matches between two word characters or two non-word characters.
DEMO
The correct regexp accepted by golang Regexp package to select tag expressions in the multiple brackets is:
rg := regexp.MustCompile(`\[([\w-]+)=([\w-]+)(?:,([\w-]+)=([\w-]+))*\]`)
Playground
See, if that was what you were looking for...
UPDATE: Just realized that it was already answered by #ndyakov.
I'm struggling to come up with the correct regex for the following scenario.
Let's say you have to match a word either starts with http- or nothing
eg : http-test-data, test-data should be a match but xyz-test-data shouldn't be a match
the regex i came up so far is
(?:http-)?(test-data)
but it matches xyz-test-data as well.
You could simply use the following:
(?:http-|^)(test-data)
This tests for either a positive look-behind of http- or for the beginning of the string before test-data.
For example, for the sample data as follows:
http-test-data
xyz-test-data
http-test-data
xyz-test-data
test-data
yes-yes-test-data
-test-data
It yeilds:
http-test-data
http-test-data
test-data
Try this representation
^(http-|)(test-data)
Yes because there is a ? on the (?:http-). Then the regex will also match any string that contains test-data.
I need to create a regex query for a Google Analytics goal. The goal url must contain:
thankYou.sjs?donate_page
However, there are many urls than can render this string, all with other modifiers that I don't care about.
Please advise.
#ExplosionPills: I think you forgot about the special meaning of the question mark.
If you don't escape it, your expression:
^thankYou.sjs?donate_path$
Would match
thankYou.sjsdonate_path
or
thankYou.sjdonate_path
Not to mention the special meaning of dot.
So I guess something like this should work:
thankYou\.sjs\?donate_path
Furthermore if it's possible that the donate_path is not the first in the query string you can use this:
thankYou\.sjs\?([^&]*&)*donate_path
Just the string itself will work. If you want only this string, just use the start/end of string zero-width assertions:
^thankYou\.sjs\?donate_path$