Regex between white space and = - regex

I am trying to write a Regex that will select everything between a whitespace and =.
From the following lines
Window x:Class="QuiddlerGUI.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
I would like it to select x:Class and xmlns. The closest I could get was this but it was not stopping at white space.
(?<=)(.*?)(?==)
I am using the regex to try and select text in a RichTextBox to attempt and change the color of the text.
foreach(TextColors color in textColors)
{
var start = body.Document.ContentStart;
while (start != null && start.CompareTo(body.Document.ContentEnd) < 0)
{
if (start.GetPointerContext(LogicalDirection.Forward) == TextPointerContext.Text)
{
var match = color.RegularExpression.Match(start.GetTextInRun(LogicalDirection.Forward));
var textrange = new TextRange(start.GetPositionAtOffset(match.Index, LogicalDirection.Forward), start.GetPositionAtOffset(match.Index + match.Length, LogicalDirection.Backward));
textrange.ApplyPropertyValue(TextElement.ForegroundProperty, color.TextColor);
start = textrange.End;
}
start = start.GetNextContextPosition(LogicalDirection.Forward);
}
}

Try with (?<=\s)[^=]*
Explanation:
(?<=\s) will look behind for a whitespace.
[^=]* will match everything until a = is met.
Regex101 Demo

Have you tried something like:
" (.*?)="
It search for a space, followed by any character until it founds an equal sign, looking for the shortest string (regexp are greedy).

Related

Regex find most center

I need to manully hyphante words that are too long. Using hyphen.js, I get soft hyphens between every syllable, like below.
I want to find the hyphen closes to the middle. All words will be more than 14 characters long. Regex that works in https://regex101.com/ or node/js example.
Basically, find the middle character excluding hyphens, check if there is a hyphen there, then step backwards one step and then forwards one step, then backwards to steps etc.
re-spon-si-bil-i-ties => [re-spon-si,-bil-i-ties]
com-pe-ten-cies. => [com-pe,-ten-cies.]
ini-tia-tives. => [ini-tia,-tives]
vul-ner-a-bil-i-ties => [vul-ner-a,-bil-i-ties]
Here's a simple js approach based on string splitting. There could be a binary search style algorithm as you mentioned which would avoid the array allocation but that seems overkill for these small data sets.
function halve(str) {
var right = str.split('-');
var left = right.splice(0, Math.ceil(right.length / 2));
return right.length > 0 ? [left.join('-'), '-' + right.join('-')] : left;
}
console.log(halve('re-spon-si-bil-i-ties'));
console.log(halve('com-pe-ten-cies.'));
console.log(halve('ini-tia-tives.'));
console.log(halve('vul-ner-a-bil-i-ties'));
console.log(halve('none')); // no hyphens returns ["none"]
You can work this out with this method:
Get middle point of string
From the middle point, and checking each character in both directions (left from middle, right from middle) check if that position is the - character. Set the index to the first such match.
If it matches that character, stop the loop and split the string on that index, otherwise return the original word.
words = [
're-spon-si-bil-i-ties',
'com-pe-ten-cies.',
'ini-tia-tives.',
'vul-ner-a-bil-i-ties',
'test',
'-aa',
'aa-'
];
split = '-'
for(word of words) {
m=Math.floor(word.length/2),offset=0,i=null
do{
if(word[m-offset] == split) i = m-offset
else if(word[m+offset] == split) i = m+offset
else offset++
}while(offset<=m && i == null)
if(i!=null && i>0) console.log([word.substring(0,i),word.substring(i)])
else console.log(word)
}
You can achieve this with:
var words = [
're-spon-si-bil-i-ties',
'com-pe-ten-cies.',
'ini-tia-tives.',
'vul-ner-a-bil-i-ties',
're-ports—typ-i-cal-ly',
'none'
];
for(var i = 0; i < words.length; ++i){
var matches = words[i]
.match(
new RegExp(
'^((?:[^-]+?-?){' // Start the regex
+parseInt(
words[i].replace( /-/g, '' ).length/2 // Round down the halfway point of this word's length without the hyphens
)
+'})(-.+)?$' // End the regex
)
)
.slice( 1 ); // Remove position 0 because it is the entire word
console.log( matches );
}
Regex explanation for re-spon-si-bil-i-ties:
^((?:[^-]+?-?){8})(-.+)$
^( - start the capture group leading up to the half way point
(?:[^-]+?-?) - find everything not a hyphen with an optional hyphen after it. Make the hyphen optional so that the second capture group can greedily claim it
{8} - 8 times; this will get us half way
) - close the half way capture group
(-.+)?$ - greedily get the hyphen and everything after it till the end of the string

Regex Replace everything except between the first " and the last "

i need a regex that replaces everything except the content between the first " and the last ".
I need it like this:
Input String:["Key:"Value""]
And after the regex i only need this:
Output String:Key:"Value"
Thanks!
You can try something like this.
patern:
^.*?"(.*)".*$
Substion:
$1
On Regex101
Explination:
the first part ^.*?" matches as few characters as possible that are between the start of the string and a double quote
the second part(.*)" makes the largest match it can that ends in a double quote, and stuffs it all in a capture group
the last part .*$ grabs what ever is left and includes it in the match
Finally you replace the entire match with the contents of the first capture group
Can you say why you need a RegExp?
A function like:
String unquote(String input) {
int start = input.indexOf('"');
if (start < 0) return input; // or throw.
int end = input.lastIndexOf('"');
if (start == end) return input; // or throw
return input.substring(start + 1, end);
}
is going to be faster and easier to understand than a RegExp.
Anyway, for the challenge, let's say we do want a RegExp that replaces the part up to the first " and from the last " with nothing. That's two replaces, so you can do an
input.replaceAll(RegExp(r'^[^"]*"|"[^"]*$'), "")`
or you can use a capturing group and a computed replacement like:
input.replaceFirstMapped(RegExp(r'^[^"]*"([^]*)"[^"]*$'), (m) => m[1])
Alternatively, you can use the capturing group to select the text between the two and extract it in code, instead of doing string replacement:
String unquote(String input) {
var re = RegExp(r'^[^"]*"([^]*)"[^"]$');
var match = re.firstMatch(input);
if (match == null) return input; // or throw.
return match[1];
}

How to get last two words written in Regex in Javascript

I am trying to get data after a colon.
This is my code:
function myFunction() {
var withBreaks = "*Cats are:* cool Pets [CATS]"
var sheet = SpreadsheetApp.getActiveSheet()
if (withBreaks) {
var tmp;
tmp = withBreaks.match(/^[\*]Cats are:[\*][\s]([a-z]+[\s]+[A-Za-z].*)$/m); //
var username = (tmp && tmp[1]) ? tmp[1].trim() : 'No username';
sheet.appendRow([username])
}
};
So I'm trying to get information after the
*Cats are:*. This code works, but, sometimes some sentences would have an asterisk and sometimes there wouldn't be an asterisk to different sentences. I would like to make one that is more unifying, if that clarifies my question a bit.
What I would like to do is, without specifying the asterisk, get data after the :. So anything after Cats are:. Do I have to specify the asterisk?
I suggest
/^\**Cats are:\**\s*([\s\S]*)/
Here, any text is captured into Group 1 with ([\s\S]*) and the asterisks are made optional with * quantifier meaning 0 or more repetitions.
See the regex demo
If the asterisks can appear 1 or 0 times, replace * with ?:
/^\*?Cats are:\*?\s*([\s\S]*)/
^ ^
See another regex demo.

Regex replace phone numbers with asterisks pattern

I want to apply a mask to my phone numbers replacing some characters with "*".
The specification is the next:
Phone entry: (123) 123-1234
Output: (1**) ***-**34
I was trying with this pattern: "\B\d(?=(?:\D*\d){2})" and the replacing the matches with a "*"
But the final input is something like (123)465-7891 -> (1**)4**-7*91
Pretty similar than I want but with two extra matches. I was thinking to find a way to use the match zero or once option (??) but not sure how.
Try this Regex:
(?<!\()\d(?!\d?$)
Replace each match with *
Click for Demo
Explanation:
(?<!\() - negative lookbehind to find the position which is not immediately preceded by (
\d - matches a digit
(?!$) - negative lookahead to find the position not immediately followed by an optional digit followed by end of the line
Alternative without lookarounds :
match \((\d)\d{2}\)\s+\d{3}-\d{2}(\d{2})
replace by (\1**) ***-**\2
In my opinion you should avoid lookarounds when possible. I find them less readable, they are less portable and often less performant.
Testing Gurman's regex and mine on regex101's php engine, mine completes in 14 steps while Gurman's completes in 80 steps
Some "quickie":
function maskNumber(number){
var getNumLength = number.length;
// The number of asterisk, when added to 4 should correspond to length of the number
var asteriskLength = getNumLength - 4;
var maskNumber = number.substr(-4);
for (var i = 0; i < asteriskLength; i++) maskNumber+= '*';
var mask = maskNumber.split(''), maskLength = mask.length;
for(var i = maskLength - 1; i > 0; i--) {
var j = Math.floor(Math.random() * (i + 1));
var tmp = mask[i];
mask[i] = mask[j];
mask[j] = tmp;
}
return mask.join('');
}

How to Split the message in as3?

Hi, am trying to split the word rtmp://xx.yyy.in/sample/test?22082208,False#&all this word.The word sample is dynamically added I don't know the count.
I want to split /sample/ how to do this kindly help me?
You want the string.split() method
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/String.html#split%28%29
var array:Array = myString.split("/"); //returns an array of everything in between /
In your case this will return
[0]->?rtmp:/ [1]->xx.yy.in [2]->sample [3]->test?22082208,False#&all
If you're looking for everything aside from the test?22082208,False#&all part and your URL will always be in this format you can use string.lastIndexOf()
var pos:int = string.lastIndexOf("/", 0); //returns the position of the last /
var newString:String = string.substr(0, pos); //creates a new string starting at 0 and ending at the last index of /
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/String.html#substr%28%29
You can do this (and almost everything) with regex:
var input:String = "rtmp://xx.yyy.in/sample/test?22082208,False#&all";
var pattern:RegExp = /^rtmp:\/\/.*\/([^\/]*)\/.*$/;
trace(input.replace(pattern, "$1")); //outputs "sample"
Here is the regex in details:
^ : start of the string
rtmp:\/\/ first string to find "rtmp://"
.* anything
\/ first slash
([^\/]) capture everything but a slash until...
\/ ...second slash
.* anything
$ the end
Then $1 represents the captured group between the parenthesis.