Regex to match surrounding text - regex

I need to replace "something[anything]" with "somethingElse(anything)", the tricky part is anything could be anything, and I don't want to replace that. Is it possible to do with regex?
P.S. In real scenario I'd be using PHPStorm to look / replace it. It would help me a lot to show me what to put exactly at the find window.
Thank you!

Idea: Capture 'anything' in a capturing group in regex. Then, back reference it in the replacement.
For example: search for something[([^]]*)\] and replace with somethingElse($1).

Related

Regex find a specific character zero or one or multiple time in a string

I'm upgrading a Symfony app with VSCode and I have numerous occurences of this kind of string :
#Template("Area:Local:delete.html.twig")
or
#Template("Group:add.html.twig")
In this case, I want to replace all the : with / to have :
#Template("Area/Local/delete.html.twig")
I can't think of doing it manually, so I was looking for a regular expression for a search/replace in the editor.
I've been toying with this fearsome beast without luck (i'm really dumb in regexp) :
#Template\("([:]*)"
#Template\("(.*?)"
#Template\("[a-zA-Z.-]{0,}[:]")
Still, I think there should be a "simple" regexp for this kind of standard replacement.
Anyone has any clue ? Thank you for any hint
You can use this regex with a capture group: (#Template.*):.
And replace with this $1/.
But you'll have to use replace all until there's no : left, that won't take long.
Just explaining a lit bit more, everything between the parenthesis is a capture group that we can reference later in replace field, if we had (tem)(pla)te, $1 would be tem and $2 would be pla
Regex!
You can use this regex #Template\("(.[^\(\:]*)?(?:\:)(.[^\(\:]*)?(?:\:)?(.[^\(\:]*)?"\) and replacement would simply be #Template\("$1/$2/$3
You can test it out at https://regex101.com/r/VfZHFa/2
Explanation: The linked site will give a better explanation than I can write here, and has test cases you can use.

Search and replace with particular phrase

I need a help with mass search and replace using regex.
I have a longer strings where I need to look for any number and particular string - e.g. 321BS and I need to replace just the text string that I was looking for. So I need to look for BS in "gf test test2 321BS test" (the pattern is always the same just the position differs) and change just BS.
Can you please help me to find particular regex for this?
Update: I need t keep the number and change just the text string. I will be doing this notepad++. However I need a general funcion for this if possible. I am a rookie in regex. Moreover, is it possible to do it in Trados SDL Studio? Or how am i able to do it in excel file in bulk?
Thank you very much!
Your question is a bit vague, however, as I understand it you want to match any digits followed by BS, ie 123BS. You want to keep 123 but replace BS?
Regex: (\d+)BS matches 123BS
In notepad++ you can:
match (\d+)BS
replace \1NEWTEXT
This will replace 123BS with 123NEWTXT.
\1 will substitue the capture group (\d+). (which matches 1 or more digits.
You could do this in Trados Studio using an app. The SDLXLIFF Toolkit may be the most appropriate for you. The advantage over Notepad++ is that it's controlled and will only affect the translatable text and not anything that might break the integrity of the file if you make a mistake. You can also handle multiple files, or even multiple Trados Studio projects in one go.
The syntax would be very similar to the suggestion above... you would:
match (\d+)BS
replace $1NEWTEXT

Nino Regular Expression

I have the following text, for example:
nino&searchPhrase=jn123456&alphabetical
And I want to extract jn123456.
I've put together the following regex to extract NINOs:
(\bnino?\b.*?|Nino?\b.*?)[a-zA-Z]{2}[0-9]{6}
The problem I have is at the very end of the regex where I'm matching the last alpha character which may or may not be there.
I've tried adding the following at the end of the regex shown above without any luck:
?[a-zA-Z]{1} and
[?a-zA-Z]{1}
Could someone please look at this and let me know where I've gone wrong.
Many thanks and kind regards
Chris
You may use something like this:
^[Nn]ino&?\w*=([a-z]{2}\d{6})
which will capture "jn123456" in the first capturing group.
Demo.
If the character & can be anything else, then you may use . instead.

Using wildcards in MS Word to replace special characters and reformat text

I'm trying to work out how to replace the following sentence:
What’s Viktoria made / makes / making in the kitchen at the moment?
with this:
What’s Viktoria [**made**makes**making] in the kitchen at the moment?
using Find & Replace with wildcards in MS Word.
Unfortunately, I'm having trouble working out a way of doing this. I've made various attempts using things like (*)(\/)(*)(\/)(*) and ([! ]*\/*\/*[! ]) but I'm getting the entire string before; i.e. it highlights everything, not just made / makes / making. I guess there might be an easier way searching for the formatting (as the only part I want to target is in italics), but any way of doing it would be greatly appreciated!
Thanks in advance. Hopefully the explanation is clear enough!
Jamie
This may be what you're after. With wildcard searching on, for the search pattern use:
(<*>) [/] (<*>) [/] (<*>)
and for the replace pattern use:
[**\1**\2**\3]
If you want to remove the italics, choose italics font format for the search pattern and regular font format for the replace text.
Hope that helps.

Extract text between two given strings

Hopefully someone can help me out. Been all over google now.
I'm doing some zone-ocr of documents, and want to extract some text with regex. It is always like this:
"Til: Name Name Name org.nr 12323123".
I want to extract the name-part, it can be 1-4 names, but "Til:" and "org.nr" is always before and after.
Anyone?
If you can't use capturing groups (check your documentation) you can try this:
(?<=Til:).*?(?=org\.nr)
This solution is using look behind and lookahead assertions, but those are not supported from every regex flavour. If they are working, this regex will return only the part you want, because the parts in the assertions are not matched, it checks only if the patterns in the assertions are there.
Use the pattern:
Til:(.*)org\.nr
Then take the second group to get the content between the parenthesis.