regex to select digits after string - regex

i need a little help with some regex, im fairly new to it all however i feel like this may not be too complex for someone who isnt panicked by regex syntax :)
I have this block of text
{ "NewMessages": "0", "NewAlarms": "0", "Alarms": "13", "PartialAlarms": "", "AckAlarms": "1", "UnusualSens": "",
And need to select the values of each category, only the digit, so far i have selected the whole string with ("Alarms": )"*"\d{1,}"however this obviously also selects Alarms and value, i need just the value of 15, however 15 will change frequently.
Any tips how to select any digits that far after "Alarms" ?
Thanks!

you use the positive lookbehind:
(?<="Alarms": )"*"\d{1,}"
this should work for you
hope it helped :)

You need to put in group.
const regex_alarms = /("Alarms": )"*"(\d{1,})"/mg;
const regex_all = /"\w+": "([0-9]+)"/mg;

Simply:
"Alarms": "(\d*)"
And your value will be in group 1. To get it:
$1

Related

regex to validate i/p string as PDC-<GroupName>-Team1 or PDC-<GroupName>-Team2 or PDC-<GroupName>-Team3

I have to build such a regex where I can validate the i/p string should be in format...
PDC--Team1
or
PDC--Team2
or
PDC--Team3
GroupName can be any alphanumeric string with only "-" allowed to use.
I am currently trying with ^PDC-[A-Za-z0-9-]+-Team1$|[A-Za-z0-9-]+-Team2$|[A-Za-z0-9-]+-Team3$ but this not working as expected..
If someone knows how to this let me know
Something like this should work for you:
^PDC-[A-Za-z0-9-]*-Team[0-9]$
So we start with PDC-, capture anything in between and end with -Team and a number.
I guessed that groups can go higher than 3, else change the last group to [1-3], If it can go higher than 9 then add a * after the group for any number.
I found it somehow working with ^PDC-[a-zA-Z0-9-]*-(Team1|Team2|Team3)$. Just still the enhancement is to allow PDC- only at start and only for single time.

VS Code if/else conditions with regular expression in user defined snippet

I am pretty bad at regex and need some help implementing my idea with the already complicated if-else syntax being used for user-defined snippets in VS Code.
I want to achieve the following:
Whenever I enter a number for variable $1 I want the snippet to create the
text "MAXVALUE $1" at the placeholder positon
if anything else is entered, there should be printed nothing
My current line for this with $1 being the variable I enter is:
"\t ${1/([0-9])|([a-zA-Z])/${1:+MAXVALUE }${2:+ }/}"
At this state I can capture the entire number EXCEPT the FRIST CHARACTER being entered and I can print MAXVALUE _mynumber_minus_char_at_index_0, LOL?!
If I enter a text, MAXVALUE won't be printed, but again the value from $1 minus the character at index 0 is being printed on screen.
Any help would be highly appreciated. If you got some useful links that explain advanced snippet creation for those kinda cases, I would be thankful as well.
For RegEx, well, time to learn them, so why not starting with a crazy-ass example like this - at least for me it is like rocket-science atm :D
Thanks in advance and best regards.
Using this snippet:
"Maxvalue": {
"prefix": "cll",
"body": [
"\t ${1/([0-9]+)|([a-zA-Z]+)/${1:+MAXVALUE }$1${2:+ }/}",
],
"description": "maxvalue"
},
([0-9]+) captures all the numbers you type; or
([a-zA-Z]+) captures all the letters you type
You were using ([0-9]) which captures, but more importantly
matches only the first number. If you don't match something it will not be transformed by the snippet transform, it just remains
untouched. That is why you were seeing everything but the first
number in the output.
You weren't actually outputting $1 anywhere - you see I added it to the transform after the MAXVALUE conditional.
${1:+MAXVALUE } is a conditional which means if there is a capture group 1, do something, in this case output MAXVALUE. That 1 in ${1:+MAXVALUE } is not a reference to your $1 tabstop. It is only a reference to the first capture group of your regex.
So you correctly outputted MAXVALUE when you had a capture group 1, but you didn't follow that up by outputting capture group 1 anywhere.
{2:+ } is anther conditional where the 2 refers to a capture group 2, if any, here ([a-zA-Z]+). So if there is a capture group 2, a space will be output. If there is no capture group 2, the conditional will fail and provide no output of its own. If you want nothing printed if you type letters, then match it and do nothing with it. As in the following:
"\t ${1/([0-9]+)|[a-zA-Z]+/${1:+MAXVALUE }$1/}", this will match all the letters you type (before tabbing to complete the transform) and they will disappear because you matched them and then didn't output them in the transform part anywhere.
If you simply want those letters to remain, don't match them as in
"\t ${1/([0-9]+)/${1:+MAXVALUE }$1/}"
If there is something you don't understand let me know.
[By the way, your question title mentions if/else conditions but you are using only if conditionals.]

Notepad++ Regex Format Replace

I'm not really a stranger to RegEx, I get the general idea behind it and have used it before with good results. But this one has my blood boiling, simply due to the amount of square brackets and single quotation marks.
I get one part working, another part breaks. I can't seem to come up with a way for it to replace it all in one go, so I thought I'd see if there are any kind souls out there that can help a desperate man.
Basically what I have are hundreds of entries that look like this;
_newObject = createVehicle ['Land_CncWall4_F', [11459.2,11330.3,-0.000549316], [], 0, 'CAN_COLLIDE'];
_newObject setPosASL [11459.4,11330.2,317.238];
_newObject setVectorDirAndUp [[0.790714,-0.612186,0], [0,0,1]];
What I want to do is, I need to get the name of the object, in this case Land_CncWall4_F. As well as the setPosASL and VectorDirAndUp co-ordinates and then format this into a new array.
The new array would look like this
["Land_CncWall4_F", [11459.4, 11330.2, 317.238], [[0.790714, -0.612186, 0], [0, 0, 1]]];
I managed to extract the object's name into a new line using backreference, which worked nicely, but if I try and get the co-ordinates as well I just get lost in my own abomination of regex.
Edit:
I did manage to make a little more progress this time, and as requested this is what I've got so far;
// Find
_newObject = createVehicle \['(.*)', \[.*], 0, 'CAN_COLLIDE'];
// Replace
\n["\1"]
// Outcome
["Land_CncWall4_F"]
Now to try and do the rest as well. Oh RegEx how I both love and utterly despise you :P
Edit2:
Thanks to all the helpful people this has now been solved! I ended up with this;
// Find
_newObject = createVehicle\s*\['(\w+)'.+\R_newObject setPosASL\s*(\[.+?\]);\R_newObject setVectorDirAndUp\s*(\[\[.+?\]\]);
// Replace
["$1", $2, $3, false],
Final output looks like this;
["Land_CncWall4_F", [11449.9,11318.2,317.056], [[0.780067,-0.625696,0], [0,0,1]], false],
Which is exactly what I wanted. Thanks everyone for your help, saved me lots of painful hair loss ;)
How about:
Find what: createVehicle\s*\['(\w+)'.+\R_newObject setPosASL\s*(\[.+?\]);\R_newObject setVectorDirAndUp\s*(\[\[.+?\]\]);
Replace with: ["$1", $2, $3]
Your input string:
_newObject = createVehicle ['Land_CncWall4_F', [11459.2,11330.3,-0.000549316], [], 0, 'CAN_COLLIDE'];
_newObject setPosASL [11459.4,11330.2,317.238];
_newObject setVectorDirAndUp [[0.790714,-0.612186,0], [0,0,1]];
becomes:
_newObject = ["Land_CncWall4_F", [11459.4,11330.2,317.238], [[0.790714,-0.612186,0], [0,0,1]]]
This might help you out:
(?s)\['(?<object>[^']+)'.*?setPosASL\s*(?<setPosASL>[^;]+).*? setVectorDirAndUp\s*(?<VectorDirAndUp>[^;]+)(?s-)
The trick is to use the single-line mode and look for the commands literally. See a demo on regex101.com. You can obviously ommit the naming groups and have them sorted as usual (1-x).
This regex _newObject setPosASL\s+(\[.*\]);$ will give the group in parentheses the required [11459.4, 11330.2, 317.238]
This regex _newObject setVectorDirAndUp\s+(\[\[.*\]\]);$ will give the group in parantheses the required [[0.790714,-0.612186,0], [0,0,1]]
Because I assume you got the Land_CncWall4_F, lets say it is group1, you can concatenate the groups like
["group1", group2, group3]
example for first one in notepad++ looks like:

Regex get between 2 string with spaces

how could I parse this response text using Regex?
info = {
"title": "Developers",
"image": "http://i.ytimg.com/vi/KMU0tzLwhbE/default.jpg",
"length": "3",
"status": "serving",
"progress_speed": "",
"progress": "",
"ads": "",
"pf": "http://70efd.pf.aclst.com/ping.php/10754233/KMU0tzLwhbE?h=882634",
"h": "87d0670f6822946338a610a6b9ec5322",
"px": ""
};
The outcome I need should look like this "87d0670f6822946338a610a6b9ec5322", however, I can't get the correct syntax. I'm new to using Regex and what I have tried using is "\s+", can anyone point me in the right direction?
If you must use a regex, you could use a regex along the lines of:
"h" : "(.+?)",
You can see an example of it here. Just read from the first capture group and that would select your text.
That looks like like JSON aside from the info = prefix. If you have any specific language you are working in that could parse JSON, that might be a better way of handling that input.
You could also use (?<="h": ")[a-z0-9]+(?="), which will match any sequence of lowercase letters and numbers, as long as the sequence is preceded by "h": " and followed by ". I made an explanation and demonstration here.

Need help removing HTML tags, certain punctuation, and ending periods

Suppose I have this test string:
test.string <- c("This is just a <test> string. I'm trying to see, if a FN will remove certain things like </HTML tags>, periods; but not the one in ASP.net, for example.")
I want to:
Remove anything contained within an html tag
Remove certain punctuation (,:;)
Period that end a sentence.
So the above should be:
c("This is just a string I'm trying to see if a FN will remove certain things like periods but not the one in ASP.net for example")
For #1, I've tried the following:
gsub("<.*?>", "", x, perl = FALSE)
And that seems to work OK.
For #2, I think it's simply:
gsub("[:#$%&*:,;^():]", "", x, perl = FALSE)
Which works.
For #3, I tried:
gsub("+[:alpha:]?[.]+[:space:]", "", test.string, perl = FALSE)
But that didn't work...
Any ideas on where I went wrong? I totally suck at RegExp, so any help would be much appreciated!!
Based on your provided input and rules for what you want removed, the following should work.
gsub('\\s*<.*?>|[:;,]|(?<=[a-zA-Z])\\.(?=\\s|$)', '', test.string, perl=T)
See Working Demo
Try this:
test.string <- "There is a natural aristocracy among men. The grounds of this are virtue and talents. "
gsub("\\.\\s*", "", gsub("([a-zA-Z0-9]). ([A-Z])", "\\1 \\2", test.string))
# "There is a natural aristocracy among men The grounds of this are virtue and talents