A regex to match a string that is surrounded by % - regex

i need a regex to find all string that is surrounded by %
example:
A=%Test1% OR B = %Test2% AND (C=%Test3
OR C = %Test4)
what i need are the strings:
%Test1%, %Test2%, %Test3% and %Test4%
thanks.

Just use
%[^%]+%
Which matches a percent sign, followed by a non-zero number of non-percent characters and another percent.

Maybe %[[:alnum::]+% would do the job?

Related

Regex match string between two characters

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);

How to remove first number from a number using regex as i want to implement in pentaho

I am getting a number as
1140302
I have to remove the first 1 from above number
140302
Actually there is a wiki page about this:
regex = ^[0-9]([0-9]+)$
replace with = $1
Above regex matches length >= 2 numeric strings, and $1 will contain the numbers without the first.
use ^. and replace with ''
demo here : http://regex101.com/r/sW8fJ7

Using regex to match certain text

I try to look for this answer for a while but no luck (sorry if I could describe it well). I am still newbie with regex. I am trying to match a string with only number and a certain delimiter. For example: the patter would be 8/16/32/64/.... the number will be split by '/' with arbitrary amount of number, I could find a way to match them.
My attempt is \d+/\d+? but couldn't get it to work.
You could remove the '/' delimiter and then test for the existence of a number
Here is some C# as an example:
static void Main(string[] args)
{
string text = "8/16/32/64/";
Console.WriteLine(text);
TestForNum(text);
text = "8/16/32/64/b";
Console.WriteLine(text);
TestForNum(text);
Console.ReadKey();
}
private static void TestForNum(string text)
{
string tmp = Regex.Replace(text, #"/", "");
Match m = Regex.Match(tmp, #"^\d+$");
if(m.Success)
{
Console.WriteLine("\t" + m.Groups[0]);
}
else Console.WriteLine("\tno match");
}
A naive approach would be
[\d/]+
However, this does match //// as well as just 12345. To match only "proper" strings:
\d+(/\d+)+
Reads digits followed by delimiter+digits repeated at least once. If trailing/leading delimiters are allowed, then
/?(\d+/)+\d*
If you're using a flavor that uses slashes to quote the regex (like javascript), you'll need to escape them:
/\d+(\/\d+)+/
You can do:
(\d+)(\D|$)
See this work That will split a list of digits delimited by any non digit, so 1?2!3.4 would match
If you want a specific delimiter, such as /:
(\d+)(?:/|$)
As simple as possible:
(\d+\/?)+
Every digit followed by [a] slash, as many as possible. You may use g flag for all matches.

MATLAB regexp skip if part of word

Given the following example:
str = 'deriv*dot(N,iv)';
expr = 'iv';
idx = regexp(str,expr);
This returns idx with 4 and 13. How do I only find the 'iv' that is NOT part of a word?
I tried messing around with Lookaround Operators for expr, but could not get the result I desired. Thanks for the help.
It seems like Matlab has it's own word boundary escape sequence.
expr = '\<iv\>';
That defines a word as anything that consists of letters, digits and underscores. If you want your own definition (i.e. letters only), then you need lookarounds:
expr = '(?<![a-zA-Z])expr(?![a-zA-Z])';

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