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:
Related
I have a variable rawvalue:
let rawvalue = {abc-def-qwe}
I want to use regex to remove the { and }; I can simply do this by truncating the first and last characters. I built the regex:
^.(.*.).$
I want to know how to apply this regex on my variable to get the desired output?
The syntax you're looking for is like this:
let input = "{abc-def-qwe}";
let re = /^.(.*.).$/;
let fixed = re.exec(input)[1]; // Get the first match group "abc-def-qwe"
Maybe, this RegEx might be a better choice, which creates one group and you can simply call it using $1 and replace your string:
^\{(.+)\}$
For implementation, you might use, maybe these posts: 1, 2, 3.
So, for example, I have this list:
10text
11text
12text
13text
14text
15text
16text
17text
18text
19text
Now I need to copy this and make it all into the 20-29 and 30-39 range with a Regex.
So only the very first 1 needs to be changed into a 2 and a 3 etc.
I can't seem to figure out what the regex is.
I tried: 1.text 1*text
Now I have been doing some reading and probably because it ain't in my native language it is still magic to me.
http://www.ntu.edu.sg/home/ehchua/programming/howto/regexe.html
https://www.regular-expressions.info/tutorial.html
http://2017.compciv.org/guide/topics/end-user-software/atom/how-to-use-regex-atom.html
Reference - What does this regex mean?
https://stackoverflow.com/tags/regex/info
What was I hoping for
When I fill in this in the search field:
1*text
and this in the replace field:
2*text
then on pressing change all the list becomes
20text
21text
22text
23text
24text
25text
26text
27text
28text
29text
I don't know exactly what you are trying to do here, but if you want to convert 1xtext to 2xtext, then try searching for this pattern:
^1
and then replace with just 2. Or, more generally, to match something like (1xtext) you could try using the pattern \b1, and again replace with 2.
Looking for a regex that can split expressions like:
A-6-b 10/%XYZ
into:
A-6-b
10%
/XYZ
Note that the first group can also contain spaces and numbers:
AQDF 100 56%/ABC
and percentage can be a float:
SFSDF 0.1%/ABC
I've come up with (^[A-Z\s\d-]*)(?!%)(\d+%)(.*$) but this doe snot match any percentages that are floats and more importantly even simple examples like ABC 10%/XYZ fail because the first digit of the percentage is assigned to the first capturing group.
Any idea how I can achieve what I want? I'm not a regex expert...
EDIT: fixed errors in example
EDIT2:
The examples are not complete. Here one more:
ABC Dwsd 0.01%/XYZ QST
First part can contain spaces
Last Part can contain spaces
number can be a float
Super simple:
/^(.*) ([1-9][0-9]*(?:\.[0-9]+)?%)(.*)$/
The most easily identifiable item is your percentage, so the ([1-9][0-9]*(?:\.[0-9]+)?%) part deals with finding that.
Then it's simply a case of getting everything before (excluding the final space) to get the name, and everything after to get the solvent.
Done.
Don't overcomplicate this by using one unreadable regex.
Based on what you've said, your separators are well defined (the last space and the last %). In JavaScript, for example, you could use:
var str = "A-6-b 10/%XYZ";
var firstSeparator = str.lastIndexOf(' ');
var secondSeparator = str.lastIndexOf('%');
var name = str.substring(0, firstSeparator);
var percentage = str.substring(firstSeparator + 1, secondSeparator + 1); // we want to include the % separator in this one
var solvent = str.substring(secondSeparator + 1);
console.log(name, percentage, solvent);
Working JSFiddle: http://jsfiddle.net/rL5uymhm/
(There may be a typo in your question, as your examples differ on where the / symbol appears. So the code may need tweaking. My point still stands – don't use a regex for the sake of it when there is a more readable alternative.)
IF you really want to use a regex, /^(.+ )([^%]+%)(.*)$/ should work.
I try this Let me know if you have any problem in comment.
((?:(?!\s*[0-9]*\/%).)*)\s*([\d\/%]*)\s*(.*)
SEE DEMO : http://regex101.com/r/lL8oN4/1
This one works for me (using PCRE):
/^(.+) ([0-9.]+)[\/%]+([^\/]+)$/
I'am quite new to regex and am trying to extract the 662050,89 from X130503XYZ662050,89 after XYZ using a regex, I tried and wrote .
[a-zA-Z](\d+|,\d)
I can only get 662050. How to get 662050,89 in regex? Thank you in advance
Please note that the XYZ can be any letter and it can be anytimes line XYZ , XXYYZ ect
The regex can be simplier
XYZ(.*)
if XYZ can be anything but numbers, use \D token
\D{3}(.*)
You may try using this reg exp (\d+,\d+). It will work just fine, if there are no float numbers within the X Y and before Z. Hope this will help.
EDIT:
Just keep in mind that the float number must be after the Z. Otherwise you may need to use [\d+,]*($|\z)
You may try matching from the end of the input / word (JavaScript):
[0-9,]*(?:$|\z)
if you use this XYZ(.*) take the group 1 of the match for the 662050,89
or if you use this ([a-zA-Z]+)(\d+[|,]\d+) take the group 2 of the match for the 662050,89.
In the first case you only care for the numbers after the 'XYY', in the second case you care for both 'XYZ' and the numbers after it.
Try this regex ...
/\w([\d\,]+)/
I had this question a couple of times before, and I still couldn't find a good answer..
In my current problem, I have a console program output (string) that looks like this:
Number of assemblies processed = 1200
Number of assemblies uninstalled = 1197
Number of failures = 3
Now I want to extract those numbers and to check if there were failures. (That's a gacutil.exe output, btw.) In other words, I want to match any number [0-9]+ in the string that is preceded by 'failures = '.
How would I do that? I want to get the number only. Of course I can match the whole thing like /failures = [0-9]+/ .. and then trim the first characters with length("failures = ") or something like that. The point is, I don't want to do that, it's a lame workaround.
Because it's odd; if my pattern-to-match-but-not-into-output ("failures = ") comes after the thing i want to extract ([0-9]+), there is a way to do it:
pattern(?=expression)
To show the absurdity of this, if the whole file was processed backwards, I could use:
[0-9]+(?= = seruliaf)
... so, is there no forward-way? :T
pattern(?=expression) is a regex positive lookahead and what you are looking for is a regex positive lookbehind that goes like this (?<=expression)pattern but this feature is not supported by all flavors of regex. It depends which language you are using.
more infos at regular-expressions.info for comparison of Lookaround feature scroll down 2/3 on this page.
If your console output does actually look like that throughout, try splitting the string on "=" when the word "failure" is found, then get the last element (or the 2nd element). You did not say what your language is, but any decent language with string splitting capability would do the job. For example
gacutil.exe.... | ruby -F"=" -ane "print $F[-1] if /failure/"