Regex match string between two characters - regex

Let's say I have a string containing a filename that includes width and height..
eg.
"en/text/org-affiliate-250x450.en.gif"
how can I get only the "250" contained by '-' and 'x' and then the "450" containd by 'x' and '.' using regex?
I tried following this answer but with no luck.
Regular Expression to find a string included between two characters while EXCLUDING the delimiters

If you are using R then you can try following solution
txt = "en/text/org-affiliate-250x450.en.gif"
x <- gregexpr("[0-9]+", txt)
x2 <- as.numeric(unlist(regmatches(txt, x)))

Use a lookbehind and a lookahead:
(?<=-|x)\d+(?=x|\.)
(?<=-|x) Lookbehind for either a - or a x.
\d+ Match digits.
(?=x|\.) Lookahead for either a x or a ..
Try the regex here.

Use the regex -(\d)+x(\d+)\.:
var str = 'en/text/org-affiliate-250x450.en.gif';
var numbers = /-(\d+)x(\d+)\./.exec(str);
numbers = [parseInt(numbers[1]), parseInt(numbers[2])];
console.log(numbers);

Related

Getting words Starting with symbol in dart

I'm trying to parse in Dart long strings containing hashtags, so far I tried various combinations with regexp but I cannot find the right use.
My code is
String mytestString = "#one #two, #three#FOur,#five";
RegExp regExp = new RegExp(r"/(^|\s)#\w+/g");
print(regExp.allMatches(mytestString).toString());
The desidered output would be a list of hahstags
#one #two #three #FOur #five
Thankyou in advance
You should not use a regex literal inside a string literal, or backslashes and flags will become part of the regex pattern. Also, omit the left-hand boundary pattern (that matches start of string or whitespace) if you need to match # followed with 1+ word chars in any context.
Use
String mytestString = "#one #two, #three#FOur,#five";
final regExp = new RegExp(r"#\w+");
Iterable<String> matches = regExp.allMatches(mytestString).map((m) => m[0]);
print(matches);
Output: (#one, #two, #three, #FOur, #five)
String mytestString = "#one #two, #three#FOur,#five";
RegExp regExp = new RegExp(r"/(#\w+)/g");
print(regExp.allMatches(mytestString).toString());
This should match all of the hashtags, placing them into capture groups for you to later use.

Regular Expression to find string between two characters

I'm trying to create a REGEX to find the string between \ and > in the following input :
\\RANDOM\APPLE\BOB\GEORGE\MIKE\TOM >>\\TEST\TEST2\TEST3\TEST\TEST\JOHN.
Desired Output:TOM
I've been able to create ([^>]+) to isolate the first section of the string before the first > . I just can't seem to figure out how to expand on this and isolate TOM.
Try
\\([^\\>]+?) >>
Regex Demo
In javascript:
let regex = /\\([^\\>]+?) >>/
// Note \\ is required for literal \ in js
let str = "\\\\RANDOM\\APPLE\\BOB\\GEORGE\\MIKE\\TOM >>\\\\TEST\\TEST2\\TEST3\\TEST\\TEST\\JOHN.";
match = str.match(regex);
console.log(match[1]); //TOM
This should works:
[^\\\s>]+(?=\s*>)
Demo:
It will works even if the desired match has one or more > after it and if has one or more whitespaces before >.
I mean: this regex will match TOM from all this strings:
\\RANDOM\APPLE\BOB\GEORGE\MIKE\TOM >\\TEST\TEST2\TEST3\TEST\TEST\JOHN.
\\RANDOM\APPLE\BOB\GEORGE\MIKE\TOM >>\\TEST\TEST2\TEST3\TEST\TEST\JOHN.
\\RANDOM\APPLE\BOB\GEORGE\MIKE\TOM>>\\TEST\TEST2\TEST3\TEST\TEST\JOHN.

Regexp within a regexp OR pattern within a match OR find commas between two strings

I want to extract commas , from this string:
"(""2018-10-15 00:00:00.571913"",147,55,2,341.920,-4.829,-1,""0,0,427799008,307238900,163872717,122358998,115140912,112840222,111386391,109396581,107696294,107176835,106021975,104275830,
But I don't want to extract ALL commas ,
Only the one situated between "", and ,""
Using https://regexr.com/ I have tried:
(?="",)(\,)(?=,"")
instead of
(?="",)(.*)(?=,"")
But it won't work.
This match output should be:
, , , , ,
In other words, in the sub-string "",147,55,2,341.920,-4.829,-1,"" I only want to extract the commas and nothing else.
PS: In need to do it in one step.
I would take a two-step approach to this.
Find what's between "",and ,"" . You were close, but unless it's different in Matlab you have 2 positive lookaheads in your description (?=) rather than a positive lookbehind (?<=) and a positive lookahead. I'd use this in Java:
(?<=["]{2},)[0-9,.\\-]+(?=,["]{2})
So a positive lookbehind preceding the pattern, then the pattern (one or more commas, periods, hyphens and/or numbers), and then a positive lookahead after the pattern. This yields: 147,55,2,341.920,-4.829,-1
Then in this String just match comma , and collect them all.
If it's for MATLAB, you can't get a single match that contains non-contiguous characters in one step. However, instead of using regexp, you could try regexprep to erase the parts you don't care about:
function testFunc()
str = '"(""2018-10-15 00:00:00.571913"",147,55,2,341.920,-4.829,-1,""0,0,427799008,307238900,163872717,122358998,115140912,112840222,111386391,109396581,107696294,107176835,106021975,104275830,';
middlePattern = '(?<=,).*?(?=,)';
beginningPattern = '^[^,]*,';
endPattern = ',".*?$';
exp = [middlePattern '|' beginningPattern '|' endPattern];
str = regexprep(str,exp,'')
>> testFunc
str =
',,,,,'
My solution doesn't contain the spaces that your desired solution has. I couldn't get that to work.
Could you expand on why you want a list of commas and what your ultimate goal is?

Simple Regular Expression matching

Im new to regular expressions and Im trying to use RegExp on gwt Client side. I want to do a simple * matching. (say if user enters 006* , I want to match 006...). Im having trouble writing this. What I have is :
input = (006*)
input = input.replaceAll("\\*", "(" + "\\" + "\\" + "S\\*" + ")");
RegExp regExp = RegExp.compile(input).
It returns true with strings like BKLFD006* too. What am I doing wrong ?
Put a ^ at the start of the regex you're generating.
The ^ character means to match at the start of the source string only.
I think you are mixing two things here, namely replacement and matching.
Matching is used when you want to extract part of the input string that matches a specific pattern. In your case it seems that is what you want, and in order to get one or more digits that are followed by a star and not preceded by anything then you can use the following regex:
^[0-9]+(?=\*)
and here is a Java snippet:
String subjectString = "006*";
String ResultString = null;
Pattern regex = Pattern.compile("^[0-9]+(?=\\*)");
Matcher regexMatcher = regex.matcher(subjectString);
if (regexMatcher.find()) {
ResultString = regexMatcher.group();
}
On the other hand, replacement is used when you want to replace a re-occurring pattern from the input string with something else.
For example, if you want to replace all digits followed by a star with the same digits surrounded by parentheses then you can do it like this:
String input = "006*";
String result = input.replaceAll("^([0-9]+)\\*", "($1)");
Notice the use of $1 to reference the digits that where captured using the capture group ([0-9]+) in the regex pattern.

Reg exp to find number

Hi I have a some text where I want to find all occurrences like the following and replace with that same number minus the apostrophes.
'1' or '164'
(pattern = apostrophe number apostrophe)
Reg Ex makes my brain sore.
Any help much appreciated.
'(16[0-4]|1[0-5][0-9]|[1-9][0-9]?)'
matches a number between 1 and 164, enclosed in apostrophes. To remove the apostrophes, replace the matched text with backreference \1.
If I understood correctly, this can help you (example using Javascript):
var x = "some_text:'68' and other:'109', finally:'05'";
var res = x.replace(/'([0-9]+)'/g,"$1");
alert(res); //some_text:68 and other:109, finally:05