Java Regex : Replacing multiple words in jsps - regex

I am having Jsp code like -
<input type = "something" name = "something" value = "dto.value" />
Now I want to replace "input" with "form:input", remove name attribute, add new attribute "path" and set "name" attribute value to path such as my final output will be -
<form:input type = "something" path = "dto.value" />
How do I achieve it because I need to do this on over 250 jsps.

If you are using Eclipse IDE, use Ctrl+F to get Find\Replace pop-up window. Then check the Regular expressions check box.
Use these Regex in the Find and Replace with textboxes respectively.
Find: <(.*?)name\s*=\s*\"([^"]*)\"(.*)/>
Replace with: <form:\1 \3 path="\2" />
Regex Explanation:
< # starting of the tag
(.*?) # group 1 - all the characters till the word 'name'
name\s*=\s* # the word 'name' and '=' with spaces in between
\" # starting of the name attribute value
([^"]*) # group 2 - the value of name attribute
\" # end of the name attribute value
(.*) # group 3 - all the other attributes and values
/> # end of the tag

This is what I did which does my job.
Find :
<input([^>]*value=")(\$\{([^"]*)\})"([^>]*)
replace with :
<form:input$1$2 path="$3" $4/

Related

Regex to search two string inside a method

How to create a regex , so as to search two strings "greet" AND inside this method string "name" .
I tried
(^.*greet(\n|.|\t)*)(.*name*)
def greet(name):
print("Hello, " + name + ". Good morning!") <--- this name should be selected
def meet(name):
print("Lets meet, " + name )
I would use this regex:
greet([^\n]|\n+[^\S\n])*name
Here the strings greet and name are separated by characters that are not a linebreak ([^\n]) or, in the case, they must be eventually followed by a space that is not a linebreak ([^\S\n]). In this way you ensure that name is in the same method of greet.
See demo.
You can capture in a group what is between the parenthesis, and use a backreference \1 in the next line to match the same.
If you want to select it, you could also capture that in a group itself.
\bdef greet\(([^\s()]+)\):\r?\n.*(\1)
Regex demo
If it should be name only
\bdef greet\([^\s()]+\):\r?\n.*\b(name)\b
Regex demo

Powershell Regex expression to get part of a string

I would like to take part of a string to use it elsewhere. For example, I have the following strings:
Project XYZ is the project name - 20-12-11
I would like to get the value "XYZ is the project name" from the string. The word "Project" and character "-" before the number will always be there.
I think a lookaround regular expression would work here since "Project" and "-" are always there:
(?<=Project ).+?(?= -)
A lookaround can be useful for cases that deal with getting a sub string.
Explanation:
(?<= = negative lookbehind
Project = starting string (including space)
) = closing negative lookbehind
.+? = matches anything in between
(?= = positive lookahead
- = ending string
) = closing positive lookahead
Example in PowerShell:
Function GetProjectName($InputString) {
$regExResult = $InputString | Select-String -Pattern '(?<=Project ).+?(?= -)'
$regExResult.Matches[0].Value
}
$projectName = GetProjectName -InputString "Project XYZ is the project name - 20-12-11"
Write-Host "Result = '$($projectName)'"
here is yet another regex version. [grin] it may be easier to understand since it uses somewhat basic regex patterns.
what it does ...
defines the input string
defines the prefix to match on
this will keep only what comes after it.
defines the suffix to match on
this part will keep only what is before it.
trigger the replace
the part in the () is what will be placed into the 1st capture group.
show what was kept
the code ...
$InString = 'Project XYZ is the project name - 20-12-11'
# "^" = start of string
$Prefix = '^project '
# ".+' = one or more of any character
# "$" = end of string
$Suffix = ' - .+$'
# "$1" holds the content of the 1st [and only] capture group
$OutString = $InString -replace "$Prefix(.+)$Suffix", '$1'
$OutString
# define the input string
$str = 'Project XYZ is the project name - 20-12-11'
# use regex (-match) including the .*? regex pattern
# this patterns means (.)any char, (*) any times, (?) maximum greed
# to capture (into brackets) the desired pattern substring
$str -match "(Project.*?is the project name)"
# show result (the first capturing group)
$matches[1]

How can I search and replace guids in Sublime 3

I have a textfile where I would like to replace all GUIDs with space.
I want:
92094, "970d6c9e-c199-40e3-80ea-14daf1141904"
91995, "970d6c9e-c199-40e3-80ea-14daf1141904"
87445, "f17e66ef-b1df-4270-8285-b3c15da366f7"
87298, "f17e66ef-b1df-4270-8285-b3c15da366f7"
96713, "3c28e493-015b-4b48-957f-fe3e7acc8412"
96759, "3c28e493-015b-4b48-957f-fe3e7acc8412"
94665, "87ac12a3-62ed-4e1d-a1a6-51ae05e01b1a"
94405, "87ac12a3-62ed-4e1d-a1a6-51ae05e01b1a"
To become:
92094,
91995,
87445,
87298,
96713,
96759,
94665,
94405,
How can i accomplish this in Sublime 3?
Ctrl+H
Find: "[\da-f-]{36}"
Replace: LEAVE EMPTY
Enable regex mode
Replace all
Explanation:
" : double quote
[ : start class character
\d : any digit
a-f : or letter from a to f
- : or a dash
]{36} : end class, 36 characters must be present
" : double quote
Result for given example:
92094,
91995,
87445,
87298,
96713,
96759,
94665,
94405,
Try doing a search for this pattern in regex search mode:
"[0-9a-z]{8}-[0-9a-z]{4}-[0-9a-z]{4}-[0-9a-z]{4}-[0-9a-z]{12}"
And then just replace with empty string. This should strip off the GUID, leaving you with the output you want.
Demo
Another regex solution involving a slightly different search-replace strategy where we don't care about the GUI format and simply get the first column:
Search for ([^,]*,).* (again don't forget to activate the regex mode .*).
Replace with $1.
Details about the regular expression
The idea here is to capture all first columns. A column here is defined by a sequence of
"some non-comma character": [^,]*
followed by a comma: [^,]*,
The first column can then be followed by anything .* (the GUI format doesn't matter): [^,]*,.*
Finally we need to capture the 1st column using group capturing: ([^,]*,).*
In the replace field we use a backreference $x which refers the the x-th capturing group.

Regex to extract string between two symbols

I have a string like this
Affiliation / Facility Name = Provider 1069860 # Admissions = 1 #
Potentially Avoidable = 0
I want a Regex Expression to extract the value Provider 1069860 from it.
I tried "= [a-zA-Z]+ #" but it is giving a blank result
With this :
.*Facility Name = ([a-zA-Z 0-9]+) #.*
You match what you want in the match group one
https://regex101.com/r/EnYZ55/1

Replacing data in CSV file with Regex

I have a CSV file (exported data from iWork Numbers) which contains of a list of users with information. What I want to do is to replace ;;;;;;;;; with ; on all lines accept "Last login".
By doing so and importing the file to Numbers again the data will (hopefully) be divided in rows like this:
User 1 | Points: 1 | Registered: 2012-01-01 | Last login 2012-02-02
User 2 | Points: 2 | Registered: 2012-01-01 | Last login 2012-02-02
How the CSV file looks:
;User1;;;;;;;;;
;Points: 1;;;;;;;;;
;Registered: 2012-01-01;;;;;;;;;
;Last login: 2012-02-02;;;;;;;;;
;User2;;;;;;;;;
;Points: 2;;;;;;;;;
;Registered: 2012-01-01;;;;;;;;;
;Last login: 2012-02-02;;;;;;;;;
So my question is what Regex code should I type in the Find and Replace fields?
Thanks in advance!
See the regex in action:
Find : ^(;(?!Last).*)(;{9})
Replace: $1;
Output will be:
;User1;
;Points: 1;
;Registered: 2012-01-01;
;Last login: 2012-02-02;;;;;;;;;
;User2;
;Points: 2;
;Registered: 2012-01-01;
;Last login: 2012-02-02;;;;;;;;;
Explanation
Find:
^ # Match start of the line
( # Start of the 1st capture group
;(?!Last) # Match a semicolon (;), only if not followed by 'Last' word.
.* # Match everything
) # End of the 1st capture group
( # Start of the 2nd capture group
;{9} # Match exactly 9 semicolons
) # End of the 2nd capture group
Replace:
$1; # Leave 1st capture group as is and append a semicolon.