I have a string right here :
const text = "12-100 12-199 12-300 12-999 12-666coucou 12-555 1-678 12c-666 plop12-100 12-199 12-300 12-999 12-666"
And 2 regex expressions :
const regex1 = /(?<=coucou).*(?=plop)/g
const regex2 = /\d{1,2}-\d{2,3}/g
I just want to match 12-555 & 1-678 with a single RegExp.
How Can I replace this .* in the first expression please?
Thank you in advance for your answers and explanations!
You can use
const regex = /(?<=coucou.*)\d{1,2}-\d{2,3}(?=.*plop)/g
See the regex demo.
JavaScript demo below:
const text = "12-100 12-199 12-300 12-999 12-666coucou 12-555 1-678 12c-666 plop12-100 12-199 12-300 12-999 12-666"
const regex = /(?<=coucou.*)\d{1,2}-\d{2,3}(?=.*plop)/g
console.log(text.match(regex));
Related
My regex is matching a string like between a tab or comma and a colon.
(?<=\t|,)([a-z].*?)(:)
This returns a string: app_open_icon:
I would like the regex to remove the : appended and return app_open_icon only.
How should I chnage this regex to exclude :?
Try (?<=\t|,)[a-z].*?(?=:)
const regex = /(?<=\t|,)[a-z].*?(?=:)/;
const text='Lorem ipsum dolor sit amet,app_open_icon:consectetur adipiscing...';
const result = regex.exec(text);
console.log(result);
You don't have to use lookarounds, you can use a capture group.
[\t,]([a-z][^:]*):
[\t,] Match either a tab or comma
( Capture group 1
[a-z][^:]* Match a char in the range a-z and 0+ times any char except :
) Close group 1
: Match literally
Regex demo
const regex = /[\t,]([a-z][^:]*):/;
const str = `,app_open_icon:`;
const m = str.match(regex);
if (m) {
console.log(m[1]);
}
To get a match only using lookarounds, you can turn the matches into lookarounds, and omit the capture group 1:
(?<=[\t,])[a-z][^:]*(?=:)
Regex demo
const regex = /(?<=[\t,])[a-z][^:]*(?=:)/;
const str = `,app_open_icon:`;
const m = str.match(regex);
if (m) {
console.log(m[0]);
}
You already have achived that with your regex.
Positive Lookbehind : (?<=\t|,) check for comma or tab before start of match
First capturing group : ([a-z].*?) captures everything before : and after , or \t
Second capturing group : (:) captures :
Just extract the first captuaring group of your match.
Check regex demo
Hello guys i'm trying to find a regular expression to match only the date in this string:
"$date" : "2019-07-10T17:34:01.222Z"
so i can get something like
2019-07-10T17:34:01.222Z
how could i achieve this
I'm guessing that this expression might just work OK:
\"\$date\"\s*:\s*"(\d{4}-\d{2}-\d{2}[A-Z]\d+:\d+:\d+\.\d+[A-Z])"
DEMO
const regex = /\"\$date\"\s*:\s*"(\d{4}-\d{2}-\d{2}[A-Z]\d+:\d+:\d+\.\d+[A-Z])"/gm;
const str = `"\$date" : "2019-07-10T17:34:01.222Z"`;
const subst = `$1`;
const result = str.replace(regex, subst);
console.log(result);
I have the following strings
/search?checkin=2018-10-25&checkout=2018-10-27&id=bandung-108001534490276290&page=1&room=1&sort=popularity&type=CITY
/search?checkin=2018-12-09&checkout=2018-12-13&id=singapore-108001534490299035&maxPrice=&minPrice=&room=1&type=REGION
/search?checkin=2018-10-22&checkout=2018-10-23&lat=-6.1176043&long=106.7767146&maxPrice=&minPrice=&room=1&type=COORDINATE
/search?page=1&room=1&type=POI&id=taman-mini-indonesia-indah-110001539700828313&checkin=2018-11-14&checkout=2018-11-16&sort=distance
i want to get all string starts from &id= until the first & so they will return
id=bandung-108001534490276290
id=singapore-108001534490299035
id=taman-mini-indonesia-indah-110001539700828313
When i tried this regex \&id=.*\& it doesn't match my requirement.
Hown do i resolve this?
I'd go with [?&](id=[^&]+).
[?&] - ? or &, because order of GET parameters is usually not guaranteed and you can get the id in the first place – something like /search?id=something-123456&checkin=2018-10-25&…
[^&]+ - at least one character that's not &
() marks a capturing group
Demo in JS:
const strings = [
"/search?checkin=2018-10-25&checkout=2018-10-27&id=bandung-108001534490276290&page=1&room=1&sort=popularity&type=CITY",
"/search?checkin=2018-12-09&checkout=2018-12-13&id=singapore-108001534490299035&maxPrice=&minPrice=&room=1&type=REGION",
"/search?checkin=2018-10-22&checkout=2018-10-23&lat=-6.1176043&long=106.7767146&maxPrice=&minPrice=&room=1&type=COORDINATE",
"/search?page=1&room=1&type=POI&id=taman-mini-indonesia-indah-110001539700828313&checkin=2018-11-14&checkout=2018-11-16&sort=distance]"
]
const regex = /[?&](id=[^&]+)/
strings.forEach(string => {
const match = regex.exec(string)
if (match) {
console.log(match[1])
}
})
Demo and explanation at Regex101: https://regex101.com/r/FBeNDN/1/
Positive Lookahead (?=)
Try a positive lookahead:
/&id=.+?(?=&)|&id=.+?$/gm
This part: (?=&) means: if an & is found, then everything before it is a match.
The alternation:| (it's an OR logic gate) is an update in regards to a comment from Nick concerning that if the parameter ended with an &id=...
It's the same match but instead of looking for a & it will look for the end of the line $. Note that the multi-line flag is used to make $ represent EOL.
Demo
var str = `/search?checkin=2018-10-25&checkout=2018-10-27&id=bandung-108001534490276290&page=1&room=1&sort=popularity&type=CITY
/search?checkin=2018-12-09&checkout=2018-12-13&id=singapore-108001534490299035&maxPrice=&minPrice=&room=1&type=REGION
/search?page=1&room=1&type=POI&id=indo-1999999051158
/search?checkin=2018-10-22&checkout=2018-10-23&lat=-6.1176043&long=106.7767146&maxPrice=&minPrice=&room=1&type=COORDINATE
/search?page=1&room=1&type=POI&id=taman-mini-indonesia-indah-110001539700828313&checkin=2018-11-14&checkout=2018-11-16&sort=distance
/search?page=1&room=1&type=POI&id=indonesia-1100055689`;
var rgx = /&id=.+?(?=&$)|&id=.+?$/gm;
var res = rgx.exec(str);
while (res != null) {
console.log(res[0]);
res = rgx.exec(str);
}
I am having some trouble getting this regular expression just right:
Sample string looks something like this:
"li3001easdasfsaasfasdi5ei1409529297ei1409529597ed16:acl_dec_tag_listl15:DEFAULT_CASE_11e18:avc_app_name_statsd29:Generic Search Engine Trafficd4:sizei5875e5:totali5ee16:Odnoklassniki.Rud4:sizei456e5:totali1ee7:Unknownd4:sizei6391e5:totali2ee5:Yahood4:sizei15673e5:totali1ee10:Yahoo Maild4:sizei5982e5:totali1e"
I want the string to be grouped like this:
(li<digit 1-4>e <string of varying length> i<single digit>e) (<string2 of varying length>)
This is my attempt at this regex so far: (li\d{1,}e.*i\de)(.*)
I would like only the first occurrence of li<digits 1-4>e as well.
Simple mistake. * is a greedy operator, meaning it will match as much as it can and still allow the remainder of the regex to match. Use *? instead for a non-greedy match meaning "zero or more — preferably as few as possible".
(li\d{1,}e.*?i\de)(.*)
val s = "li3001easdasfsaasfasdi5easdasfsafas"
val p = """li(\d{1,4})e([^i]*)i(\d)(.*)""".r
val p(first,second,third,fourth) = s
results in:
first: String = 3001
second: String = asdasfsaasfasd
third: String = 5
fourth: String = easdasfsafas
Not sure if that answers your question, but hope it helps.
I have a string something like "[aaa][vad][adf]", i would like to use regex to capture the data in[], and chars in [] can be char and number and no length limit. I am regex noob, can anyone help me on this?
Thanks.
You can try something like this:
var data = "[asd][dfhg][asfsa243]";
var re = new Regex(#"\[([^\]]*)\]");
var matches = re.Matches(data);
for (int i = 0; i < matches.Count; i++ )
{
var m = matches[i];
Console.WriteLine(m.Groups[1]);
}
Console.ReadLine();
This outputs:
asd
dfhg
asfsa243
The regular expression \[([^\]]*)\] matches zero or more occurrences of a character that isn't the ] character and which is between a pair of square brackets ([ and ])
This regex might work for you:
\[(\w+)\]
That if you by char refers to word character
Here is my suggestion:
\[(\w+)\]
The charachter will be provided in the first group of the match, like this:
var regex = new Regex(#"\[([\w\d]*)\]");
MatchCollection matchCollection = regex.Matches(stringToTest);
foreach (Match match in matchCollection)
{
Debug.Print(match.Groups[0].Value);
}
Here are some good resources for building regex's
http://www.regexlib.com
http://regexpal.com/
Here is the regex for Alphanumeric:
^[a-zA-Z0-9]+$
Here's how to ensure there are 3:
/^([a-zA-Z0-9]){3}$/
You could try -
[^\[\]\W]+
var matches = System.Text.RegularExpressions.Regex.Matches("[aaa][vad][adf]",#"[^\[\]\W]+").Cast<Match>().Select(m => m.Value);
That should ignore any '[', ']' and non-word characters and would return 'aaa', 'vad' and 'adf' from your example string.