course renaming

A swapping-ground for Regular Expression syntax

course renaming

Postby HomerSimpsons » Sat Oct 13, 2018 9:55 pm

Hello expert regex experts, have been trying to figure out why the tool won't accept my regex. Maybe i'm going something wrong, i don't know. First time ever making a regex tbh.

But anyways; I have made this:
Code: Select all
([0-9]*)+([.]) (\w.*)
and on https://regexr.com/ it matches my courses just fine. Here are some examples:
Code: Select all
1. Getting Started with iOS 12 and Swift 4.2
24. iOS Interface Builder - The $999 App


It matches the numbers at group 1, the dot as 2 and the rest of the text as group 3. But for some reason in the tool I can't use \1 and get the numbers. But \2 and \3 works just fine for the dot and text tho?

Maybe I'm missing something I don't know, please let me know. Thanks in advance!
HomerSimpsons
 
Posts: 3
Joined: Sat Oct 13, 2018 9:49 pm

Re: course renaming

Postby Goof » Sun Oct 14, 2018 4:01 am

What you need is probably something like this:

Code: Select all
^(\d+)\. (.*)$

Group 1 will be the number, without a dot, group 2 will be the course name, all the way to the end of the string. To elaborate a bit:

"^" Anchors the expression at the start of the string. This means the regex engine won't try to find partial matches starting halfway through a string.

"(\d+)" Specifies a capturing group. "\d" matches any digit, shorthand for "[0-9]". Then we have a "+", which says to match one or more of the preceding tokens, which was the digit in this case. As soon as the engine meets a character that's not a digit, the capturing group is complete and we move on.

"\. " Matches the literal character ".", followed by a space. Without the "\" in front, the engine will interpret "." as a wildcard and match any character, which in this case might not be what you want. If some course numbers don't have the dot after the number, making the dot optional like "\.? " might work better here.

"(.*)$" Is the second capturing group. The ".*" is a wildcard dot that matches any character, followed by the star "*", which will tell the engine to match 0 or more of the preceding dot character. This will keep matching any character until it hits the "$" anchor, which means "end of string".
Goof
 
Posts: 1
Joined: Sun Oct 14, 2018 3:45 am

Re: course renaming

Postby therube » Sun Oct 14, 2018 2:37 pm

(Far from an "expert") but starting with what you had...

Code: Select all
^([0-9]+)(\.\s)(.*)


\1, your digits
\2, ". "
\3, all else

Also note that you need not necessarily group something like the DOT, if you didn't want to, as you can "hard code" that in the replacement.

Code: Select all
Match:  ^([0-9]+)\.\s(.*)
Replace:  \2 -.- \1

Code: Select all
1. Getting Started with iOS 12 and Swift 4.2
24. iOS Interface Builder - The $999 App

Code: Select all
Getting Started with iOS 12 and Swift 4.2 -.- 1
iOS Interface Builder - The $999 App -.- 24
therube
 
Posts: 1314
Joined: Mon Jan 18, 2016 6:23 pm

Re: course renaming

Postby HomerSimpsons » Tue Oct 16, 2018 10:58 am

I have tried both of the examples posted, and non of them seemed to work for me I have no clue if I do something wrong or what is happening..

But anyways I was playing around with it a bit more, and got to this solution:
Code: Select all
([\d]{0,2})([.]) (\w.*)
- and that works flawlessly for me. But I still group the dot in group 2. But that don't matter much, and I want to replace the dot with a dash.

But thanks for the replays, gave me some more inside on how this thing works!
HomerSimpsons
 
Posts: 3
Joined: Sat Oct 13, 2018 9:49 pm

Re: course renaming

Postby Panchdara » Wed Oct 17, 2018 9:48 pm

Homer - what exactly are you trying to rename:
1. Getting Started with iOS 12 and Swift 4.2
24. iOS Interface Builder - The $999 App

to??????? what would you want the final result to be?
Panchdara
 
Posts: 67
Joined: Sat Jan 09, 2016 7:25 pm

Re: course renaming

Postby HomerSimpsons » Mon Oct 22, 2018 8:16 am

Sorry didn't mention what I wanted the final renaming to be. It should be something like this:

Code: Select all
1. Getting Started with iOS 12 and Swift 4.2 -> Lesson 01 - Getting Started with iOS 12 and Swift 4.2
24. iOS Interface Builder - The $999 App -> Lesson 24 - iOS Interface Builder - The $999 App


This is for my plex course library. But has some courses that don't have the (dot) in the course name. but only a number and the course name;
Code: Select all
1 Getting Started with iOS 12 and Swift 4.2 ( ... )


So i really want something that can handle both of the examples. And still output the wanted Lession naming scheme.

Best regards.
HomerSimpsons
 
Posts: 3
Joined: Sat Oct 13, 2018 9:49 pm


Return to Regular Expressions


cron