What would be the regex emulating GitHub's autolinked references?
It takes Markdown on input and outputs enriched Markdown where strings like #123 are converted to [#123](https://github.com/owner/repo/issues/123).
These are some examples of the transformations that I'd like the regex to do:
Input:
1. #123
2. https://github.com/owner/repo/issues/123
3. https://github.com/shoptet/sofa/pull/456
4. owner/repo#123
5. https://github.com/owner/repo/issues/123#issuecomment-123456789
Output:
1. [#123](https://github.com/owner/repo/issues/123)
2. [#123](https://github.com/owner/repo/issues/123)
3. [#123](https://github.com/owner/repo/pull/456)
4. [owner/repo#123](https://github.com/owner/repo/issues/123)
5. [#123 (comment)](https://github.com/owner/repo/issues/123#issuecomment-123456789)
I'd prefer one giant regex if possible (I know it's not going to be nice but would allow me to process Markdown in a couple of my favorite editors directly).
If you don't mind changing the format a little (using [#123-comment] instead of [#123 (comment)] for comments), you may use this:
(?:(owner/repo)?#(\d+)\b|https?://github\.com/([^/]+/[^/]+/(?:issues|pull))/(\d+)(#issue(comment)(-)\d+)?)
Replace by: [\1#\2\4\7\6](https://github.com/owner/repo/issues/\2\4\5)
You have a demo here.
I'd still prefer a (complex) regex but if anyone is looking for the same post-processing like me, this package can solve it in a Node.js script:
https://github.com/remarkjs/remark-github
Related
Is it possible to look for numbers in a text file and do some math evaluation on them in the replace feature ? Using a built-in feature or a plugin.
For example, suppose I have following text file content :
1
2
3
4
5
And I want to increment each number, for a result like this :
2
3
4
5
6
It's possible that PackageControl contains a Sublime Text 2 package that would do something like this. Some quick searching found Evaluate and Selection Evaluator (which is deprecated) which can both replace the selection by doing something external and may be workable for your use case.
The Emmet package also has an Evaluate Math Expression functionality that may be useful, if you happen to already use Emmet in your workflow. A note on Emmet is that it's fairly intrusive and steals your Tab key, so extra configuration may be required to keep other things working as you expect.
Although you've tagged Sublime Text 2 specifically, I'd also point out that the latest development versions of Sublime Text 3 have among their list of new features something like the following, which may be what you want.
Here I use a simple regex that matches all numbers, then use the new Arithmetic command to manipulate the values using an arbitrary expression.
Something to note is that this is a feature added in the latest development series and hasn't made its way to stable yet. As such if you want to use it right now you need to have an ST3 license (only licensed users can use development versions).
I enabled vintage mode on sublime text.. but there are some important vim commands that are lacking.. so let's say I want to do a search and replace like so
:10,25s/searchedText/toReplaceText/gc
so I wanna search searchedText and replace it with toReplaceText from lines 10 to 25 and be prompted every time (ie yes/no)..
how do I do this with Sublime Text? everytime I hit : it gives me this funny menu.. any way around that?
If you so much would like to see vim in action, try the other way around; ie enable sublime stuff in vim.
Here are 2 links that might come in handy:
subvim and vim multiple cursors (Which is one amazing feature in sublime that lacks in native vim).
Hope that gets you creative ;)
Unfortunately vintage mode does not understand ranges. The best way I know how to do this is with incremental search:
highlight the first occurrence of searchedText on line 10
hit cmnd/ctrl D to have Sublime find the next occurence
If you you want the next occurrence ignored, hit cmnd/ctrl K
Once you have highlighted all the occurrences, you can replace them all at once, as Sublime has left cursors behind on every occurrence you opted in on.
VintageEx gives you a Vim-like command-line where you can at least perform substitutions. Well, that's how far I went when trying it. I don't know how extended the subset of Vim commands it implements is but I'd guess that it's not as large as the original and, like with Vintage, probably different and unsettling enough to keep a relatively experienced Vimmer out.
Anyway, I just tried it again and indeed you can more or less do the kind of substitution you are looking for, which instantly makes ST a lot more useful:
:3,5s/foo/bar/g
:.,5s/bar/foo/g
:,5/foo/bar/g
:,+5/bar/foo/g
Unfortunately, it doesn't support the /c flag.
a plugin named vintageous offers more features including search function. It's available in package control
although this question is answered.. i figured this would add some value
the full functionality of vi search/replace is possible with the ruby mine IDE, once you install the ideavim plugin. The idea is perfect for ruby on rails by the way.
I have one .ics file from which I would like to create individual new .ics files depending on the event categories (I can't get egroupware to export only events of one category, I want to create new calendars depending on category). My intended approach is to repeatedly eliminate all events but those of one category and then save the file using EditPad Lite 7 (Windows).
I am struggling to get the regular expression right. .+? is still too greedy and negating the string (e.g. to eliminate all but events from one category) doesn't work either.
Sample
BEGIN:VEVENT
DESCRIPTION:Event 2
END:VEVENT
BEGIN:VEVENT
DESCRIPTION:Event 3
CATEGORIES:Sports
END:VEVENT
BEGIN:VEVENT
DESCRIPTION:Event 4
END:VEVENT
The regex BEGIN:VEVENT.+?CATEGORIES:Sports.+?END:VEVENT should only match sports events but it catches everything from the first BEGINto the first ENDfollowing the category.
Edit: negating doesn't work either: BEGIN:VEVENT.+?((?!CATEGORIES:Sports).).+?END:VEVENT.
What am I missing? Any pointers are highly appreciated.
I guess newlines are removed or ignored, because your regex does not care about them.
I only have a correction to the match after CATEGORIES
BEGIN:VEVENT.+?CATEGORIES:Sports.*?END:VEVENT
^
Zero or more
The first part of your regex looks good, maybe the regex engine in EditPad is not so good.
Try it with a different editor or scripting language (like Eclipse or perl or Notepad+ or Notepad2)
You could split the input and then grep the matching Sports events
#sportevents = grep /Sports/, split /END:VEVENT/, $input
map $_.="END:VEVENT", #sportevents
This was perl, maybe you can launch a script from EditPad to do it.
The second line just restores the END:VEVENT that was stripped during split.
OK. Solved it. I found something here which can be used to split ics files. I tweaked it to use the category rather than the summary in the file name and then merged the individually generated files according to category. I added the usual ics header and footer to all files and, voilĂ , I had individual calendar files.
Hi I'm using Redmine to write a wiki of my software. I need to put some notes next to a code section like this:
class.method()[1]
Where the "one" is a link to my note at the end of the page.
I've tried to use any method defined in the Textile syntax but it seems that it doesn't work. In fact when you use the code tag '# #' any other tag stops working.
It's good even if I can use the link tag [[ ]] but only if it is like this google.com
Thanks for any help,
Alessandro
Redmine uses Coderay to parse the code sections in the Wiki. Take a look at the documentation for the different languages. Otherwise I would suggest using comments instead of footnotes or in worst case line references to the code.
The footnote will only work if there is an alphanumeric character directly before the opening square bracktet:
this[1], whereas this()[2] or this [3]
fn1. will work
fn2. won't work.
fn3. won't work.
At least this is true with Redmine 3.1. See this issue for more information.
Note that you need blank lines between fn1., fn2. and fn3. to get a correct rendering.
This extension for Redmine supports footnotes and custom styles in the wiki.
Redmine supports Textile markup syntax. Textile has support for footnotes. As noted on ticket ticket #974, this is the syntax for using footnotes in Redmine:
Text with a footnote[1]
fn1. and here the actual footnote.
I need a to split a string of the form
2,9.1,The Godfather (1972), (it's a csv line)
to:
2
9.1
The Godfather
1972
any ideas for a good regular expression?
BTW,
if you know a good regular expressions creator based on examples you provide it'd be great.
I'm a bit new to this..
10x!!
(\d+)\.(\d+\.\d+),(.*?)(?= \()\((\d{4})\)
^^^^^ ^^^^^^^^^^ ^^^^^^^^^^^^ ^^^^^^^
2 9.1 Title Year
I wouldn't recommend using regex to split the csv files as it can't handle comma escaping well. But having that said, how about using the simplest available solution?
A simplest regex like this should solve your problem
'(.*?),(.*?),(.*?)\((\d+)\)'
A little time with Google gave me this: /,(?!(?:[^",]|[^"],[^"])+")/. Seeems to split CSV just fine.
>>> '2,9.1,The Godfather (1972)'.split(/,(?!(?:[^",]|[^"],[^"])+")/)
["2", "9.1", "The Godfather (1972)"]
If you are sure that the format is static, you can use this:
(\d+),(\d+\.\d+),(.*?) \((\d+)\)
But if it can contain more information, use a real CSV parser to read the line and then just split The Godfather (1972) using (.*?) \((\d+)\).
CSV has a lot of corner cases, your regexp approach might take you into a world of pain.
For example if the title has a comma in it, the title would then be double quoted. Which would screw up with all of the regexps given so far.