Find and replace with regex?

Running into an issue with code formatting that can be found easily with a regex expression: “:\S” I would like to replace with a regex expression, but Xcode cannot seem to do this. I find that Atom cannot either, is there free alternative software option to replace a term with a regex expression so that I could add a space to any matching criteria without manually having to do so?

e.g.

let vol:Bool = false

becomes

let vol: Bool = false

A simple space insert to the right of a character?

Find in Xcode defaults to case-insensitive plain text. It can also match using regex. You probably need check it out in the (left) side bar.

I haven’t tried replace, but it should work much the same.

Thank you for your prompt response.

I can search with regex values, but not replace.

We can always fall back to ye good ol’ days

sed -i~ -E 's/:(\S)/: \1/g' filename
- E extended regex
- i editing inplace

As with any other match-replace operations, do be careful.

Appending ~ after -i to have it backup to filename~.

1 Like

Click “Find” to get a drop‐down where you’ll also see “Replace”.
Click “Text” to get a drop‐down where you’ll also see “Regular Expression”.

Capture%20d%E2%80%99%C3%A9cran%202019-07-01%20%C3%A0%2020%20h%2027%20min%2051%20s

Or in only one file (after ⌘F):

Click “Find” to get a drop‐down where you’ll also see “Replace”.
Click “Contains” to get a drop‐down where you’ll also see “Regular Expression”.

Lantua, you're a diamond!

I had to rely on the online regex101.com but it worked perfectly, thank you.

Thanks also to SDGGiesbrecht, but I could not get Lantua's revolution to work in Xcode natively, is there a way, if not I think I'll suggest it to Apple. It would be great if importing C++ or Perl scripts that profiles could be loaded to remove such issues like semi-colons etc..

Glad to be of help :smile:.

I planned on it to just be small command line though. Didn't think you'll need online regex to invoke it. sed should be installed on macOS & most linux distro, and they should readily support -E and -i.

No, I don't think there's a way. These commands generally don't have much to interact with a GUI, it just take a line, edit a file, exit.

If you want to use that frequently, you probably want to save it to some script file so that you can use them easily later. It'd be useful for Xcode to run scripts easily, but that'd be hard call.

With that said, if you really want to do styling consistently, I'd suggest that you use linter, eg. SwiftLint, or you can probably fine a good one out there.

Terms of Service

Privacy Policy

Cookie Policy