Regex & Sublime: Replacestring - regex

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.

Related

Find number of Instances for few words in string while ignoring other few words using regex

Hi i am using regex in Matlab.
I need to find number of hits for few words while ignoring other few words using regex
what i have tried so far:
String = 'Sunday:Monday:Tuesday:Wednesday:Thursday:Friday:Saturday:Sun:Mon:Tue:Wed:,Thu:,Fri:,Sat:';
Output = regexp( String,'^(?!.*(,Sun:|,Sunday:)).*(Sun:|Sunday:)' )
The Output of above regexp comes as true, But need it as 2 as it got hit 2 times for Sun: and Sunday:.
In next Scenario:
String = 'Sunday:Monday:Tuesday:Wednesday:Thursday:Friday:Saturday:Sun:Mon:Tue:Wed:,Thu:,Fri:,Sat:';
Output = regexp( String,'^(?!.*(,Fri:|,Friday:)).*(Fri:|Friday:)' )
The Output of above regexp comes as false, But need it as 1 as it*** got hit 1 time*** for Friday:.
I also tried:
regexp( String,'^(?!.*(,Sun:|,Sunday:)).*(Sun:|Sunday:)' ,'match')
But its giving Output as whole string.
I am confused how to get number of hits while ignoring other words, Help would be appreciated regexp work in Matlab same as normal.
You can use
(?<!,)Fri(?:day)?:
It matches
(?<!,) - a location not immediately preceded with ,
Fri - Fri
(?:day)? - an optional day string
: - a colon.
See the regex demo.
If you allow some redundancy, you may build the pattern like this:
(?<!,)(Fri:|Sunday:)
It will match Fri: or Sunday: not immediately preceded with a comma.
Unless you really need to use regexp, something like this will be easier to maintain:
Output = sum(ismember(strsplit(String,':'),{'Sunday','Sun'}))

Need to match the repeated words and replace it with a new one using regex

I was trying to match a pattern from the below line in linux,
$(menu_no),ini_question3.vox,inv_question3.vox,inv_question3.vox,ini_question3.vox,to_question3.vox
From the above line i need to find the repeated word and replace the repetition with some other word,
Ex: Here the repeated word is
inv_question3.vox,inv_question3.vox
i need this to be changed to
inv_question3.vox,end.vox
I was trying to do a find and replace in vim editor using the below command, but it didn't work,
:s/\(inv_question*.vox\),\(inv_question*.vox\)/\1,end.vox/g
I'm not sure what you want to replace, but if you want to match duplicate string inv_question3.vox, you can try:
let string = '$(menu_no),ini_question3.vox,inv_question3.vox,inv_question3.vox,ini_question3.vox,to_question3.vox';
let result = string.replace(/(inv_question\d\.vox\,){2,}/, '$1,end.vox,');
console.log(result)
\d: only 1 number.
{2,}: 2 or more times
Then, replacing with $1,end.vox, with $1 is inv_question3.vox

vim regex multiline: search works, match() doesn't

Let's say I have this buffer:
a
b
c
(
1
2
3
)
#
#
$
I would like, in a vimscript to get the contents of line between parentheses.
/(\n\(.\n\)*)
highlights exactly what I want. But I don't succeed to get this with something like:
let pattern = '(\n\(.\n\)*)'
match(getline(1, '$'), pattern)
I try a lot of stuffs, such as:
match(join(getline(1,'$'), '\n'), pattern)
, even double quotes for pattern, but nothing works... Any idea ?
(my aim is not necessary to make this match() works, but just to get the result from a buffer to a vimscript)
With your first try (match(getline(1, '$'), pattern)), Vim tries to find the pattern inside each line; as your pattern is multi-line, it never matches.
So, your second try goes to a right direction, because you try to join the lines, then the pattern would effectively match... Unless you use '\n' as a glue for the join : this string is litterally replaced by a backslash \ followed by an n character. Just replace single quotes by double quotes, then special chars will be parsed.
So, this version will work better:
echo matchstr(join(getline(1,'$'), "\n"), pattern)

Keep the RegExp search value and add

How do I return the value I searched for in RegExp and then add my modifier?
For example the following RegExpression will search for any word between the comma's.
[^,\s][^\,]*[^,\s]*
The following is my input:
Biscuit, Pudding, Pizza, Beans
However I can't find a way to add a word like "Cheese-" to those words. The expected output would be:
Cheese-Biscuit, Cheese-Pudding, Cheese-Pizza, Cheese-Beans
Do a CTRL+H in Notepad++, in replace window search mode block, check Regular Expression.
then in Find what field type : ([^,\s][^\,]*[^,\s]*) and in Replace with field type: Cheese-\1 then replace all, you'll see this result:
Cheese-Biscuit, Cheese-Pudding, Cheese-Pizza, Cheese-Beans
Update-you have changed to say notepad++. I will leave in case useful to someone else.
Assuming Javascript (since you mention RegExp):
var str = "Biscuit, Pudding, Pizza, Beans";
var patt1 = /[^,\s][^\,]*[^,\s]*/g;
var result = str.replace(patt1,"Cheese-" + "$&");
Output:
Cheese-Biscuit, Cheese-Pudding, Cheese-Pizza, Cheese-Beans
$& inserts the matched substring.
/g does global match - all matched words.
See here for more information.

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+)