How would I write this regex pattern in lua with no libraries? - regex

I would like to write this regular expression pattern:
(?<=bring a ).*?(?=,)
in lua but have no idea on how one would do so. If someone could point me in the right direction that'd be appreciated!
Regular expression pattern explanation: it's supposed to grab anything in between bring a and ,

Lua patterns aren't as advanced as regex, so you can't write a pattern that does exactly what the regex you posted does. However, you can write a pattern that does what this regex does:
bring a (.*?),
The difference being that you get the result you want in a subgroup, instead of as the whole match. Here's how you do that:
bring a (.-),
And you can use it like this:
str = 'I will bring a ball, so if you bring a bat, then we can have batting practice.'
for s in str:gmatch('bring a (.-),') do
print(s)
end
That prints ball and bat.

Related

Using a regular expression to insert text in a match

Regular Expressions are incredible. I'm in my regex infancy so help solving the following would be greatly appreciated.
I have to search through a string to match for a P character that's not surrounded by operators, power or negative signs. I then have to insert a multiplication sign. Case examples are:
33+16*55P would become 33+16*55*P
2P would become 2*P
P( 33*sin(45) ) would become P*(33*sin(45))
I have written some regex that I think handles this although I don't know how using regex I can insert a character:
The reg is I've written is:
[^\^\+\-\/\*]?P+[^\^\+\-\/\*]
The language where the RegEx will be used is ActionScript 3.
A live example of the regex can be seen at:
http://www.regexr.com/39pkv
I would be massively grateful if someone could show me how I insert a multiplication sign in middle of the match ie P2, becomes P*2, 22.5P becomes 22.5P
ActionScript 3 has search, match and replace functions that all utilise regular expressions. I'm unsure how I'd use string.replace( expression, replaceText ) in this context.
Many thanks in advance
Welcome to the wonder (and inevitable frustration that will lead to tearing your hair out) that is regular expressions. You should probably read over the documentation on using regular expressions in ActionScript, as well as this similar question.
You'll need to combine RegExp.test() with the String.replace() function. I don't know ActionScript, so I don't know if it will work as is, but based on the documentation linked above, the below should be a good start for testing and getting an idea of what the form of your solution might look like. I think #Vall3y is right. To get the replace right, you'd want to first check for anything leading up to a P, then for anything after a P. So two functions is probably easier to get right without getting too fancy with the Regex:
private function multiplyBeforeP(str:String):String {
var pattern:RegExp = new RegExp("([^\^\+\-\/\*]?)P", "i");
return str.replace(pattern, "$1*P");
}
private function multiplyAfterP(str:String):String {
var pattern:RegExp = new RegExp("P([^\^\+\-\/\*])", "i");
return str.replace(pattern, "P*$1");
}
Regex is used to find patterns in strings. It cannot be used to manipulate them. You will need to use action script for that.
Many programming languages have a string.replace method that accepts a regex pattern. Since you have two cases (inserting after and before the P), a simple solution would be to split your regex into two ([^\^\+\-\/\*]?P+ and P+[^\^\+\-\/\*] for example, this might need adjustment), and switch each pattern with the matching string ("*P" and "P*")

vim regex: Match with negation in middle of pattern

I am trying to learn regular expressions in vim and have gotten stuck on the following task:
I want to make a substitute which matches all of the following lines - and similar - except for the one containing Look aXXhead. With similar I mean just anything in between the a and the head except XX.
Look ahead
Look aYhead
Look aXhead
Look aXXhead
Look aXXXhead
In case it is relevant I had my highest hopes when trying
:%s/Look a\(XX\)\#!head/*\0
:%s/Look a\(.*\&\(XX\)\#!\)head/*\0
which only matches Look ahead due to the zero width of the \(XX\)\#! I pressume.
Tries like
:%s/Look a\(XX\)\#!.*head/*\0
misses the "triple X" line.
So, how is it done?
P.S. This is my first post on stack exchange so please help and correct me in case there is better ways or places for this question.
Try with this pattern:
/\(Look aXXhead\)\#!\&Look a.*head/
It's made of tho parts:
\(Look aXXhead\)\#!
match at every position in the file BUT not at /Look aXXhead/
Look a.*head
match also /Look aXXhead/
The \& operator tells that each of these parts have to match at the same pos.
how about:
/\vLook a(XXhead)#!.*head
or without very magic:
/Look a\(XXhead\)\#!.*head

Regex pattern not matching for integers followed by strings

I want to create a regex that would start with a integer number and then it might have a colon followed by a string. For example, it should pass for:
123
123:e43e
123:444+:343
I tried using the regex as:
String timeZoneRegex = "^\\d+[:(=[a-zA-Z+-:0-9]+)]*";
This does not work; appreciate any help here.
I have to say that some regexp features depend on the regexp engine, but try with:
\d+(\:[a-zA-Z0-9\-+]+)*
I've given a look to your express, you've made some mistake, maybe the most relevat one is the use of embeded [], you should know that inside the squared brackets the behaviour of symbols intepretation is a little different. This is a very good source if you want to learn them. Cheers.

Dart capturing parentheses in replaceAll

I'm working on a Dart program that will parse data from an XML file. Unfortunately, the file is a bit of a mess, so I'm doing a ton of regex on it to get it into decent shape. Since Dart, like Javascript, doesn't have lookbehind functionality, I've been trying to use capturing parentheses along with the method to mimic lookbehind, to no avail. Dart doesn't like the syntax of it at all, just telling me that function is not defined for myClass
My XML file consists of strings in the following format:
<items>Chicken Sauces/ Creamy Curried Chicken Salad w/ Wild Rice</items>
When finished, I want that string to look like:
<items>Chicken Sauces| Creamy Curried Chicken Salad w/ Wild Rice</items>
I've tried calling this on the string: .replaceAll(new RegExp(r'(\w\s*)/(?=\s*\w)'),"$0|$1")
but that gives me errors saying "Expected an identifier" for $0 and $1. If anyone could offer me some pointers on how to properly use capturing parentheses, or a method to mimic lookbehind in Dart, along with the current lookahead, I'd be very grateful!
Assuming your regexp is correct you should use String.replaceAllMapped: .replaceAllMapped(new RegExp(r'(\w\s*)/(?=\s*\w)'), (match) => "${match[0]}|${match[1]}")
You might also be interested in String.splitMapJoin
Compeek already pointed out that the regexp probably won't do what you want, though.

Match all cases between string and comma using Regex

I have a large string. Here is a part of it:
{"status":"ok","items":[{"image_versions":[{"url":"http:\/\/distilleryimage8.instagram.com\/11a67042c62311e1bf341231380f8a12_7.jpg","width":612,"type":7,"height":612},{"url":"http:\/\/distilleryimage8.instagram.com\/11a67042c62311e1bf341231380f8a12_6.jpg","width":306,"type":6,"height":306},{"url":"http:\/\/distilleryimage8.instagram.com\/11a67042c62311e1bf341231380f8a12_5.jpg","width":150,"type":5,"height":150}],"code":"MrMBxJo-O8","has_more_comments":true,"taken_at":1341438972.0,"comments":[{"media_id":228329104165036988,"_spam":false,"text":"I live in Oklahoma! :D Shoot them off with me! :D","created_at":1341441914.0,"user":{"username":"heather_all_over","pk":13296276,"profile_pic_url":"http:\/\/images.instagram.com\/profiles\/profile_13296276_75sq_1339538236.jpg","full_name":"Heather\ud83c\udf80","is_private":false},"content_type":"comment","pk":228353791620276525,"type":0},{"media_id":228329104165036988,"_spam":false,"text":"Wish I had that much money to spend.......","created_at":1341441916.0,"user":{"username":"l_mcnair","pk":23775741,"profile_pic_url":"http:\/\/images.instagram.com\/profiles\/profile_23775741_75sq_1339894045.jpg","full_name":"Lauryn","is_private":true},"content_type":"comment","pk":228353803204944174,"type":0},{"media_id":228329104165036988,"_spam":false,"text":"You should video tape you setting them all off","created_at":1341441939.0,"user":{"username":"ahrii_","pk":37732021,"profile_pic_url":"http:\/\/images.instagram.com\/profiles\/profile_37732021_75sq_1340907381.jpg","full_name":"Ahriana;-*","is_private":false},"content_type":"comment","pk":228353997065675057,"type":0},{"media_id":228329104165036988,"_spam":false,"text":"When did skrillex start selling
I am trying to match every number after "pk":". I have been trying look aheads but can't quite seem to get it right. I don't know much about regex so if somebody could point me in the right direction that would be great!
This looks like a JSON response. Why not just parse the JSON and pull out the values for all the "pk" keys?
Depending on what language you're using, the regex might look different, but this should work on most languages:
/"pk":(\d+)/g
That basically looks for the string "pk": and then all the digits after that, placing those digits in a capturing group. The g at the end makes it search for all occurrences. Depending on the language you're using though, you might not be able to retrieve all of captures.
If you want the part after something you should use look-behind:
(?<="pk":)\d+