I need to split a string to one or more substrings each of which contains no more or less than two dots. For example, if the string is foo.boo.coo.too" then what would be the regex to get the following array?: ["foo.boo.coo", "boo.coo.too"]. I hope there will be someone to answer this question - I will really admire you, as I've been programming for several years and have not still be used to regular expressions well enough to solve this particular problem by myself. Thank you very much in advance. Let me know your identity so that I can credit you as a contributor of the program I am creating.
RegEx is for this Problem not the best solution a similar problem was discussed here: split-a-sting-every-3-characters-from-back-javascript
A good javascript solution would be a javascript function like this
function splitter(text){
var parts = text.split(".");
var times = parts.length - 2;
var values = [];
for(var index = 0; index<times;index++)
{
values.push(parts.slice(index,index+3).join("."));
}
return values;
}
splitter("too.boo.coo.too")
//=> Result tested on Chrome 25+ ["too.boo.coo", "boo.coo.too"]
I hope this helps
If you want to Use Regex try the Lookhead Stuff, this could help http://www.regular-expressions.info/lookaround.html
Regex by its nature will return non-intersecting results, so if you want "all matches" from a single regex - it's not possible.
So basically you will need to find first match, and then start from next position to find next match and so on; something like this technique described here regex matches with intersection in C# (it's not JavaScript but idea is the same)
You can use the following regex for example:
(?<=^|\.)((?:[^.]*\.){2}[^.]*?)(?=$|\.)
It ensures that it starts and ends with dot, or at begin/end of line, and contains exactly two dots inside, and captures result in first capture. You can replace * with + to make sure at least one symbol exists between dots, if it is required.
But you need to understand that such approach has really bad performance for the task you are solving, so may be using other way (like split + for) will be better solution.
Related
I have a task and I can't solve it (i.e. do it): A string is given, for example '011125228'. Delete the first, sixth and last characters from it.
In the resulting string, check that the sum of the first three digits equals the sum of the second three digits.
If so, output 'yes', otherwise output 'no'.
My beginning of this code:
var str = '011125228'
var y = str.replace (/^.|.$/g ,'')
var s = y.replace(y[6])
console.log (s)
I'm beginner in coding, pls help me!!!
Thanks for earlier
I want to shorten and finish this task. And also solve the problem with this code (
Please do not use regex for something as simple as this. Regex is extremely powerful but also extremely difficult to read, if you are doing something as simple as this there is probably a built in function in js that can do it. In your case you want to use str.substring(N)
I assume you need an output of 22 within the input of 011125228 in this case your code will look something like this.
let str = '011125228'
let newStr = str.substring(6, str.length - 1)
I hope you can code in if statements yourself.
P.S. Please try to avoid var they can be useful in rare cases but in most they cause unnecessary variable scope pollution.
I wish to ask, is it possible to use regexes as delimiters in PapaParse? Something like:
Papa.parse(string,
{
delimiter:regex
}
);
I am trying to match a specific pattern of CSVs like so:
/([A-Za-z]{2}[0-9]+,?)/g
i.e. I want exactly 2 letters, any amount of numbers, and a comma (or not, in the case of the last element).
Since string.split has a wonderful habit of returning anything but null when nothing matches regex patterns, I was hoping that my answer would lie in PapaParse. If this is not possible, then I would do something more long winded, but hopefully I can be laz-... efficent this time. :)
Trying to do the following:
Papa.parse('ACB5,dsa',{delimiter:'[A-Za-z]{2}[0-9]+,?'});
Results in
["ACB5","dsa"]
Thank you for your time.
edit
Trying out the regex on regexr.com shows that it works with values like
AB544444444444,BC5,
aa5,
At this point, I realize that this was actually a dozy question, considering how a delimiter is the thing that separates what you want to break up.
I'm writing the longer winded version now, so I'll stick that up soon
As Matt (and common sense) rightly say, yes, The delimiter is just ye olde comma. I was looking for a way to separate the results based on a regex, which past me had thought would have some similarity to how string.split works. This is the snippet I was trying to shrink down.
var result = null;
var regex = /([A-Za-z]{2}[0-9]+,?)/g; //Any two letters, a number, and a space
$(document).ready( function() {
$('#inputGraphText').on('input', function(e){ //on text input...
result = $(this).val().split(','); //split using the complex delimiter ','. Also adds a few "" elements to the array for some reason.
var tidy = new Array();
result.forEach(function(elem){
if(elem.search(regex) > -1){
tidy.push(elem.replace('/[, ]/g',''));//Tidy up the result
}
});
$('#first').html(tidy); //place to print out the tidied results
})
});
Obviously , this is not terribly schnazzy (and completely misses out on using PapaParse), but it is what I originally set out to do.
Any better alternatives will take pride of place, but for now, this is fine.
My apologies for the confusion.
Can anyone help me?
I want to split a hex string on "0000", but this "0000" must be followed by anything other than "00". I'm trying to Split (), but any solution serves me.
EDIT: Explaining ... and correcting a few things: P
This is an example of hexstring I'm using.
http://pastebin.com/u68bG6PP (It is a coded text in Shift-JIS (with some peculiarities example below))
"82824f4f00000000828250500000000082825151000000008282525200000000828253530000000082825454000000008282555500000000"
Here "0000" indicates that is the end of the text line, so it should be split in the last "0000" before the next line (which never begins with "00").
Basically necessary that the above excerpt turn:
82824f4f0000
828250500000
828251510000
828252520000
828253530000
828254540000
828255550000
And that, I hope the explanation is good now ee.
And extra question, I never really touched with Shift_JIS, so any way to turn it into unicode for displaying, or simply display it as SJIS is welcome.
You can use a regex with negative lookahead:
yourHex.split(/0000(?!00)/g)
This is an explicit translation of your problem description. However it might not necessarily be what you want, because it yields (maybe) surprising results:
"10000001".split(/0000(?!00)/g)
// => ["10", "01"]
If you want the four zeroes to not be preceded by another zero, you might have to use another technique, since JS regexes do not support lookbehind.
If I understand, you want to split on "0000" but you want to leave "000000" alone?
SO for example, "00001111000000222200003233" would result in
["11110000002222","333"]?
"00001111000000222200003333".replace(/000000/g,"token")
.split("0000")
.map(function (el) {
return el.replace(/token/g,"000000");
});
//["11110000002222","333"]
negative lookahead will yield a different result
"00001111000000222200003333".split(/0000(?!00)/)
//["", "11110", "02222", "3333"]
not sure what you are looking for exactly though
I think your question need more explaining and maybe some code to back it up. I think this is what you are asking for.
yourHex.split(/0000/);
Hi need to extract ONE letter from a string.
The string i have is a big block of html, but the part where i need to search in is this text:
Vahvistustunnus M :
And I need to get the M inside the nbsp's
So, who is the quickest regex-guru out there? :)
Ok, according to this page in the molybdenum api docs, the results will be all of the groups concatenated together. Given that you just want the char between the two 's then it's not good enough to match the whole thing and then pull out the group. Instead you'll need to do something like this:
(?<=Vahvistustunnus )[a-zA-Z](?= )
Warning
This might not work for you because lookbehinds (?<=pattern) are not available in all regex flavors. Specifically, i think that because molybdenum is a firefox extension, then it's likely using ECMA (javascript) regex flavor. And ECMA doesn't support lookbehinds.
If that's the case, then i'm gonna have to ask someone else to answer your question as my regex ninja (amateur) skills don't go much further than that. If you were using the regex in javascript code, then there are ways around this limitation, but based on your description, it sounds like you have to solve this problem with nothing but a raw regex?
Looks like it uses JavaScript and if so
var str = "Vahvistustunnus M :";
var patt = "Vahvistustunnus ([A-Z]) :";
var result = str.match(patt)[1];
should work.
I am trying to parse a string in this format
[something](something something) [something](something something)
and I want to break on every space that is not between a set of parenthesis?
I tried using js string.split with this as the regex /[^\(].*\s+.*[^\)]/g, but it doesn't work? Any suggestions appreciated :-)
EDIT: I don't want to post this as an answer, because I want to leave it open to comments but I finally found a solution.
var a = "the>[the](the the) the>[the](the the) the"
var regex = /\s+(?!\w+[\)])/
var b = a.split(regex)
alert(b.join("+++"))
Is your input always this consistent? If it is, it could be as simple as splitting your string on ') ['
If it isn't, is it possible to just take what is between [ and )? Or is there some kind of nesting that is going on?
You are using the wrong tool for the job.
As was alluded to in this famous post, regular expressions cannot parse non-regular languages, and the "balanced parenthesis" problem cannot be described by a regular language.
Have you tried writing a parser instead?
EDIT:
It seems that you've finally clarified that nesting is not a requirement. In that case, I'd suggest gnur's solution.
This regex will do exactly what you asked, and nothing more:
'[x](x x) [x](x x)'.split(/ +(?![^\(]*\))/);