remove everything after 4th digit using regular expression ruby - string

string="A.Brilliant.Young.Mind.2014.720p.BluRay.x264.YIFY"
I have a string like the one above, i am trying to prettify all my file names. I want to remove everything after the 4th digit found in the string so the name looks like this
"A.Brilliant.Young.Mind.2014"
so far using regular expression I can only remove everything before the numbers not after with the code below
string=string[/[^0-9]+/]

this worked for me. Thank you once again and please be more considerate next time
string.match(/[a-zA-Z0-9. ]*\d{4}/)

Related

How do I extract a particular value from a string with a condition using regex [duplicate]

I'm trying to catch the last part after the last backslash
I need the \Web_ERP_Assistant (with the \)
My idea was :
C:\Projects\Ensure_Solution\Assistance\App_WebReferences\Web_ERP_WebService\Web_ERP_Assistant
\\.+?(?!\\) // I know there is something with negative look -ahead `(?!\\)`
But I can't find it.
[Regexer Demo]
Your negative lookahead solution would e.g. be this:
\\(?:.(?!\\))+$
See it here on Regexr
One that worked for me was:
.+(\\.+)$
Try it online!
Explanation:
.+ - any character except newline
( - create a group
\\.+ - match a backslash, and any characters after it
) - end group
$ - this all has to happen at the end of the string
A negative look ahead is a correct answer, but it can be written more cleanly like:
(\\)(?!.*\\)
This looks for an occurrence of \ and then in a check that does not get matched, it looks for any number of characters followed by the character you don't want to see after it. Because it's negative, it only matches if it does not find a match.
You can try anchoring it to the end of the string, something like \\[^\\]*$. Though I'm not sure if one absolutely has to use regexp for the task.
What about this regex: \\[^\\]+$
If you don't want to include the backslash, but only the text after it, try this: ([^\\]+)$ or for unix: ([^\/]+)$
I used below regex to get that result also when its finished by a \
(\\[^\\]+)\\?$
[Regex Demo]

Doing two string manipulations in puppet

I am running puppet 3.8.6
In a template, I need to truncate the last four characters then remove hyphens from a string parameter. For example "foo-bar.txt" should become "foobar".
val[0..-5] works for truncating the last four characters.
val.gsub('-','') works for removing the hyphen.
But this is a syntax error.
val[0..-5].gsub('-','')
How can I do both?
I agree with the comment on your post... I don't think your example would produce a syntax error. However, though this is a little more verbose, I find splitting to be easier to reason about than removing slices of the string. This ought to work, too:
val.split('.')[0].gsub('-','')
Edit: I somehow missed that this was inside a template. Oops! I've updated as Alex Harvey suggested in the comments.

Complex find and replace in notepad++

I'm trying to do a find and replace in notepad++ where i remove the dashes from a set of numbers and letters formatted as following: aa-aaa-nn-nnnnn
I've considered writing a plugin, but it just seems like too much work to be worth it.
Here's an example of what I have and what I need.
I have this: <ISRC>AB-CED-12-34567</ISRC>
And the result should be: <ISRC>ABCED1234567</ISRC>
I've tried finding (A-Z+)-(A-Z+)-(\d+)-(\d+) and replacing this with \1\2\3\4
but then it can't find the "text". If I knew how to write the darned search codes, I could do this myself, but I just can't find a complete guide anywhere.
You're close, you want to use character class:
Ctrl+H
Find what: ([A-Z]+)-([A-Z]+)-(\d+)-(\d+)
Replace with: $1$2$3$4
Replace all
[A-]+ means one or more uppercase letter, if you want to match also lowercase, use [A-Za-z]+

Add parenthesis to vim string

I'm having a bit of trouble using parenthesis in a vim string. I just need to add a set of parenthesis around 3 digits, but I can't seem to find where I'm suppose to correctly place them. So for example; I would have to place them around a phone number such as: 2015551212.
Right now I have a strings that separates the numbers and puts a hyphen between them. For example; 201 555-1212. So I just need the parenthesis. The final result should look like: (201) 555-1212
The string I have so far is this: s/\(\d\{3}\)\(\d\{3}\)/\1 \2-/g
How might I go about doing this?
Thanks
Just add the parens around the \1 in your replacement.
s/\(\d\{3\}\)\(\d\{3\}\)/(\1) \2-/g
If you want to go in reverse, and change "(800) 555-1212" to "8005551212", you can use something like this:
s/(\(\d\d\d\))\ \(\d\d\d\)-\(\d\d\d\d\)/\1\2\3/g
Instead of the \d\d\d, you could use \d\{3\}, but that is more trouble to type.

Complex replacement in gVim

I have been a terrible person as of late when it comes to Minecraft. I have over-modded it to the point that I need to completely re-write the IDs of them all.
The only problem is that... It'll take about a couple of hours jut to re-write them ONCE, not to mention if any of them collide with the original game. So, in order to save time, I figured I'd use Vim, but after reading through several of the helpful posts on here, I still only know a minimal amount about the replacement feature/command. Here's what I'm trying to do:
Replace this
I:exampleModnamePath.id=16389
I:exampleModnamePat2.id=19657
Etc.
With this
I:exampleModnamePath.id=20000
I:exampleModnamePath.id=20001
Etc.
This continues for a while, and to those who answer, could you please inform me of how it works, so I don't have to ask these questions all the time?
For your perusal:
:let g:num = 1
:g/\.id=\d\+$/exec 's!\.id=\d\+$!.id='.g:num.'! | let g:num=g:num+1'
This is slightly simplified version of my code for (re)numbering chapters in the ebooks.
Idea in a nutshell: use :g to run something over affected lines; use :exec to generate/run new substitution command AND increment the counter. Tried it once and was surprised to find that the trick worked. Was inspired by my previous toying with :g//s/// combo.
I'm not sure what is the rule you are using to choose which number to use for replacement but if all you need
is just a new number that doesn't collide with previous ones you could try just replacing the first digit
with something in a range not used. Something like replacing 16389 with 76389
To do that you could use this :s/Path.id=.\(.*\)/Path.id=7\1
That would search for the string Path.id= followed by a single character and then a group of more characters.
I will replace it with the string Path.id=7 and the group previously selected.
You could make it more selectiv adding letters before Path.id to match only certain types of paths.

Resources