Vim find and replace everything after '=' until end of line ($) - regex

I have a line of strings (10 to 20):
title = models.CharField(max_length=250)
...
field20 = models.DateField()
How can I use Vim's find and replace to remove everything from the equal sign until the end of line, so the text turns into:
title
...
field20
I'm familiar with the visual select find and replace (:s/\%Vfoo/bar/g), so I'd like to do something like (:s/\%V< = until $>//g), but this is what I am unsure how to find in Vim:
= until $
I looked at the vimregex wiki, and tried the following with no success:
:s/\%V = (.*?)$//g

Are you looking for this:
:%s/=.*/
?
It's the same as
:%s/=.*//
but the ending / can be omitted. You don't need the g btw because the pattern to the end of the line can match just once.
If you also want to remove the whitespace before the = use:
:%s/[[:blank:]]*=.*/
Note: If you think [[:blank:]] is hard to type, you can use \s instead. Both stand for space or tab.

Related

Regex: Remove empty sections from INI (Text) files where the files could contain multiple such sections

I have below resultant php.ini file, after I was able to remove the comments & spaces thro' some simple Regex find/replace steps:
[PHP]
engine = On
short_open_tag = Off
....
....
[CLI Server]
cli_server.color = On
[Date]
[filter]
[iconv]
[imap]
[intl]
[sqlite3]
[Pcre]
[Pdo]
[Pdo_mysql]
pdo_mysql.default_socket=
[Phar]
[mail function]
SMTP = localhost
smtp_port = 25
mail.add_x_header = Off
[ODBC]
....
....
[dba]
[opcache]
[curl]
curl.cainfo = "{PHP_Dir_Path}\extras\curl\cacert.pem"
[openssl]
[ffi]
As you can see, there are multiple occurrences where multiple empty sections(sections which doesn't contain any semicolon-less(non-commented) lines) in this file, and I can't bring myself to make a regex find/replace pattern that could let me remove all such empty sections in one go, so that it becomes like below:
[PHP]
engine = On
short_open_tag = Off
....
....
[CLI Server]
cli_server.color = On
[Pdo_mysql]
pdo_mysql.default_socket=
[mail function]
SMTP = localhost
smtp_port = 25
mail.add_x_header = Off
[ODBC]
....
....
[curl]
curl.cainfo = "{PHP_Dir_Path}\extras\curl\cacert.pem"
Can anyone help me out achieve, what I need ?
An idea to look ahead after lines starting with [ for another opening bracket (or end).
^\[.*+\s*+(?![^\[])
Here is the demo at regex101 - If using NP++ uncheck: [ ] . dot matches newline
^ line start (NP++ default)
\[ matches an opening bracket
.*+ any amount of any characters besides newline (without giving back)
\s*+ any amount of whitespace (also possessive to reduce backtracking)
(?! negative lookahead to fail on the defined condition ) which is:
[^\[] a character that is not an opening bracket
In short words it matches lines starting with [ up to eol and any amount of whitespace...
if there is either no character ahead or the next character is another [ opening bracket.
Side note: Its positive equivalent is ^\[.*+\s*+(?=\[|\z) where \z matches end of string.
You can try to match if there is a ] followed by a new line and then a [, with the following regex:
\]\n\[
EDIT:
As pointed by your comment, that would just get the ][ characters, so you could try this instead:
(\[(\w)+\]\n)(?!\w)
This will match a title that is not followed by a word in the next line.
EDIT2:
My previous answer would not get the last section if it was empty, so I changed it to check the newline OR end of file.
(\[(\w)+\])(\n(?!\w)|$)
You need to tell your regex-engine to use the single-line aka "dotall" mode. Then you can easily pick out any bracketed strings that are only separated by a newline:
/\[[^\]]+\]\s\[[^\]]+\]/gs
The s flag enables "dotall" mode.
Update: Overlooked one obvious problem with my solution. It gets a bit more complicated now, using a lookahead (?:\s(?=\[)). Also extra caution needs to be taken to capture the last empty section, which is done with the |$ part. Regexr link updated...
/\[[^\]]+\](?:\s(?=\[)|$)/gs

Regex & Sublime: Replacestring

I have around 500 lines of this in my model:
var jobTitle: FieldInfoModel? = null
I want to make each line to be like this:
#Json(name = "job_title") var jobTitle: FieldInfoModel? = null
I am very noob in regex.
Im planning to copy all the lines in Sublime and do the replacement magic there.
Anyone can help me what to put in the Search and Replace fields?
I can't think of a one-liner regex solution for working out the problem but can provide a two-step process in order to reach it:
Step 1
Find: var\s*(\w+)
Replace with: #Json(name = "\1") $0
Step 2
Find: (#Json\(name = "|(?!\A)\G)([a-z]+)([A-Z])
Replace with: \1\2_\L\3
Notes:
\L Causes all subsequent characters to be output in lower case, until a \E is found.
\U Causes all subsequent characters to be output in upper case, until a \E is found.

startWith with regex kotlin [duplicate]

I am trying to work on regular expressions. I have a mainframe file which has several fields. I have a flat file parser which distinguishes several types of records based on the first three letters of every line. How do I write a regular expression where the first three letters are 'CTR'.
Beginning of line or beginning of string?
Start and end of string
/^CTR.*$/
/ = delimiter
^ = start of string
CTR = literal CTR
$ = end of string
.* = zero or more of any character except newline
Start and end of line
/^CTR.*$/m
/ = delimiter
^ = start of line
CTR = literal CTR
$ = end of line
.* = zero or more of any character except newline
m = enables multi-line mode, this sets regex to treat every line as a string, so ^ and $ will match start and end of line
While in multi-line mode you can still match the start and end of the string with \A\Z permanent anchors
/\ACTR.*\Z/m
\A = means start of string
CTR = literal CTR
.* = zero or more of any character except newline
\Z = end of string
m = enables multi-line mode
As such, another way to match the start of the line would be like this:
/(\A|\r|\n|\r\n)CTR.*/
or
/(^|\r|\n|\r\n)CTR.*/
\r = carriage return / old Mac OS newline
\n = line-feed / Unix/Mac OS X newline
\r\n = windows newline
Note, if you are going to use the backslash \ in some program string that supports escaping, like the php double quotation marks "" then you need to escape them first
so to run \r\nCTR.* you would use it as "\\r\\nCTR.*"
^CTR
or
^CTR.*
edit:
To be more clear: ^CTR will match start of line and those chars. If all you want to do is match for a line itself (and already have the line to use), then that is all you really need. But if this is the case, you may be better off using a prefab substr() type function. I don't know, what language are you are using. But if you are trying to match and grab the line, you will need something like .* or .*$ or whatever, depending on what language/regex function you are using.
Regex symbol to match at beginning of a line:
^
Add the string you're searching for (CTR) to the regex like this:
^CTR
Example: regex
That should be enough!
However, if you need to get the text from the whole line in your language of choice, add a "match anything" pattern .*:
^CTR.*
Example: more regex
If you want to get crazy, use the end of line matcher
$
Add that to the growing regex pattern:
^CTR.*$
Example: lets get crazy
Note: Depending on how and where you're using regex, you might have to use a multi-line modifier to get it to match multiple lines. There could be a whole discussion on the best strategy for picking lines out of a file to process them, and some of the strategies would require this:
Multi-line flag m (this is specified in various ways in various languages/contexts)
/^CTR.*/gm
Example: we had to use m on regex101
Try ^CTR.\*, which literally means start of line, CTR, anything.
This will be case-sensitive, and setting non-case-sensitivity will depend on your programming language, or use ^[Cc][Tt][Rr].\* if cross-environment case-insensitivity matters.
^CTR.*$
matches a line starting with CTR.
Not sure how to apply that to your file on your server, but typically, the regex to match the beginning of a string would be :
^CTR
The ^ means beginning of string / line
There's are ambiguities in the question.
What is your input string? Is it the entire file? Or is it 1 line at a time? Some of the answers are assuming the latter. I want to answer the former.
What would you like to return from your regular expression? The fact that you want a true / false on whether a match was made? Or do you want to extract the entire line whose start begins with CTR? I'll answer you only want a true / false match.
To do this, we just need to determine if the CTR occurs at either the start of a file, or immediately following a new line.
/(?:^|\n)CTR/
(?i)^[ \r\n]*CTR
(?i) -- case insensitive -- Remove if case sensitive.
[ \r\n] -- ignore space and new lines
* -- 0 or more times the same
CTR - your starts with string.

Regex to match all lines starting with a specific string

I have this very long cfg file, where I need to find the latest occurrence of a line starting with a specific string. An example of the cfg file:
...
# format: - search.index.[number] = [search field]:element.qualifier
...
search.index.1 = author:dc.contributor.*
...
search.index.12 = language:dc.language.iso
...
jspui.search.index.display.1 = ANY
...
I need to be able to get the last occurrence of the line starting with search.index.[number] , more specific: I need that number. For the above snippet, that number would be 12.
As you can see, there are other lines too containing that pattern, but I do not want to match those.
I'm using Groovy as a programming/scripting language.
Any help is appreciated!
Have you tried:
def m = lines =~ /(?m)^search\.index\.(\d+)/
m[ -1 ][ 1 ]
Try this as your expression :
^search\.index\.(\d+)/
And then with Groovy you can get your result with:
matcher[0][0]
Here is an explanation page.
I don't think you should go for it but...
If you can do a multi-line search (anyway you have to here), the only way would be to read the file backward. So first, eat everything with a .* (om nom nom)(if you can make the dot match all, (?:.|\s)* if you can't). Now match your pattern search\.index\.(\d+). And you want to match this pattern at the beginning of a line: (?:^|\n) (hoping you're not using some crazy format that doesn't use \n as new line character).
So...
(?:.|\s)*(?:^|\n)search\.index\.(\d+)
The number should be in the 1st matching group. (Test in JavaScript)
PS: I don't know groovy, so sorry if it's totally not appropriate.
Edit:
This should also work:
search\.index\.(\d+)(?!(?:.|\s)*?(?:^|\n)search\.index\.\d+)

help with regex - extracting text

Suppose I have some text files (f1.txt, f2.txt, ...) that looks something like
#article {paper1,
author = {some author},
title = {some {T}itle} ,
journal = {journal},
volume = {16},
number = {4},
publisher = {John Wiley & Sons, Ltd.},
issn = {some number},
url = {some url},
doi = {some number},
pages = {1},
year = {1997},
}
I want to extract the content of title and store it in a bash variable (call it $title), that is, "some {T}itle" in the example. Notice that there may be curly braces in the first set of braces. Also, there might not be white space around "=", and there may be more white spaces before "title".
Thanks so much. I just need a working example of how to extract this and I can extract the other stuff.
Give this a try:
title=$(sed -n '/^[[:blank:]]*title[[:blank:]]*=[[:blank:]]*{/ {s///; s/}[^}]*$//p}' inputfile)
Explanation:
/^[[:blank:]]*title[[:blank:]]*=[[:blank:]]*{/ { - If a line matches this regex
s/// - delete the matched portion
s/}[^}]*$//p - delete the last closing curly brace and every character that's not a closing curly brace until the end of the line and print
} - end if
title=$(sed -n '/title *=/{s/^[^{]*{\([^,]*\),.*$/\1/;s/} *$//p}' ./f1.txt)
/title *=/: Only act upon lines which have the word 'title' followed by a '=' after an arbitrary number of spaces
s/^[^{]*{\([^,]*\),.*$/\1/: From the beginning of the line look for the first '{' character. From that point save everything you find until you hit a comma ','. Replace the entire line with everything you saved
s/} *$//p: strip off the trailing brace '}' along with any spaces and print the result.
title=$(sed -n ... ): save the result of the above 3 steps in the bash variable named title
There are definitely more elegant ways, but at 2:40AM:
title=`cat test | grep "^\s*title\s*=\s*" | sed 's/^\s*title\s*=\s*{?//' | sed 's/}?\s*,\s*$//'`
Grep for the line that interests us, strip everything up to and including the opening curly, then strip everything from the last curly to the end of the line