Regex to extract specific words - regex

I am looking for regex to extract following words from text. For example, the word which comes after "Replaced" in each of these lines:
Replaced disk
Replaced floppy
Replaced memory
Please suggest the regex for it.

We can't really help you without more details (like which regex flavor you're using, for example), but you probably want to match it with something like this:
Replaced\s+(\w+)\b
...and then extract the desired portion from capturing group #1.

use this regex (?<=Replaced\s*)(.+)

I think
/Replaced\s(\w*?)/
Or:
/(?<=Replaced\s)(\w*)/
If you only want to select the word and not Replaced.

Related

Regex - Find the Shortest Match Possible

The Problem
Given the following:
\plain\f2 This is the first part of the note. This is the second part of the note. This is the \plain\f2\fs24\cf6{\txfielddef{\*\txfieldstart\txfieldtype1\txfieldflags144\txfielddataval44334\txfielddata 35003800380039000000}{\*\txfielddatadef\txfielddatatype1\txfielddata 340034003300330034000000}{\*\txfieldtext 20{\*\txfieldend}}{\field{\*\fldinst{ HYPERLINK "44334" }}{\fldrslt{20}}}}\plain\f2\fs24 part of the note.
I'd like to produce this:
\plain\f2 This is the first part of the note. This is the second part of the note. This is the third part of the note.
What I've Tried
The example input/output is a very simplified version of the data I need to parse and it would be nice to have a way to parse the data programmatically. I have a PHP application and I've been trying to use regex to match the segments that are important and then filter out the parts of the string that aren't required. Here's what I've come up with so far:
/\\plain.*?\\field{\\\*\\fldinst{ HYPERLINK "(.*?)" }}{\\fldrslt{(.*?)}}}}\\plain.*? /gm
regex101: https://regex101.com/r/ILLZU6/2
It almost matches what I want, but it but grabs the longest possible match instead of the shortest. I want it to match only one \\plain before the \\field{.... Maybe after the \\plain, I could match anything except for a space? How would I go about doing that?
I'm no regex expert, but my use-case really calls for it. (Otherwise, I'd just write code to handle everything.) Any help would be much appreciated!
(?:(?!\\plain).)* will match any string unless it contains a match for \\plain. Here's the regex implementing this:
/\\plain(?:(?!\\plain).)*\\field{\\\*\\fldinst{ HYPERLINK "(.*?)" }}{\\fldrslt{(.*?)}}}}\\plain.*? /gm
regex101: https://regex101.com/r/ILLZU6/5
Also, you can replace the space at the end with (?: |$) if you want to allow the end of the text to trigger it as well as a space:
/\\plain(?:(?!\\plain).)*\\field{\\\*\\fldinst{ HYPERLINK "(.*?)" }}{\\fldrslt{(.*?)}}}}\\plain.*?(?: |$)/gm
regex101: https://regex101.com/r/ILLZU6/4

How to group a certain character in regex expression?

This is my regex expression
/(')([\w\ \,\"]+)(')/g
I want to group the \" individually so I can replace it with some other character later, how do I do that?
Sample https://regex101.com/r/mqO4yL/1
Kind of hard to know for sure without an example of the text you'd being using in the regex. But based on what it looks like it's trying to do if you had something like:
/'"test"'/g
or
/'test"'/g
You could use:
/(')((\w|\ |\,)*(\")*)*(')/g
and the fourth group would get you the double quotes. There might be an easier way without so many groups but without an example text I'm not sure.
My guess is that we want to capture " only and everything else then replace it. Maybe, this expression would be an option:
([\s\S]*?)(")?
Demo

How to replace all lines based on previous lines in Notepad++?

I have an XML code:
<Line1>Matched_text Other_text</Line1>
<Line2>Text_to_replace</Line2>
How to tell Notepad++ to find Matched_text and replace Text_to_replace to Replaced_text? There are several similar blocks of code, with one exactly Matched _text and different Other_text and Text_to_replace. I want to replace all in once.
My idea is to put
Matched_text*<Line2>*</Line2>
in the Find field, and
Matched_text*<Line2>Replaced_text</Line2>
in the Replace field. I know that \1 in regex might be useful, but I don't know where to start.
The actual code is:
<Name>Matched_text, Other_text</Name>
<IsBillable>false</IsBillable>
<Color>-Text_to_replace</Color>
The regex you're looking for is something like the following.
Find: (Matched_text[\w,\s<>\/]*<Color>-).*(</Color>)
Replace: \1Replaced_text\2
Broken down:
`()` is how you tell regex that you want to keep things (for use in /1, /2, etc.), these are called capture groups in regex land.
`Matched_text[\w,\s<>\/]*` means you want your anchor `Matched_text` and everything after it up till the next part of the expression.
`<Color>-).*(</Color>)` Select everything between <Color>- and </Color> for replacement.
If you have any questions about the expression, I highly recommend looking at a regex cheatsheet.

Regular expression to find a text as being a whole word

I am using the ABAP statement READ REPORT and I want to use FIND ALL OCCURRENCES OF REGEX. Let's say for example I want to search for SELECT but when I do FIND ALL OCCURRENCES OF REGEX 'SELECT', the return table gets lines that have SELECT-OPTIONS, SELECTION-SCREEN and SELECT.
How do I use regex to get only those lines with SELECT, discarding the other 2 possible matches in the example above?
Just go for `SELECT `
Note the extra space and the use of grave quotes (grave quotes so that the trailing space is considered). This simple solution is feasible because it's highly unlikely that there's a new line right after SELECT.
Your requirement is so simple that you don't need to use a regular expression.
http://sapignite.com/regex-in-abap/
OR
Download a PDF from this Link
http://www.google.co.in/url?sa=t&rct=j&q=how%20do%20i%20use%20regex%20in%20abap%20to%20search%20for%20a%20specific%20string%3F&source=web&cd=1&ved=0CCMQFjAA&url=http%3A%2F%2Fwww.sdn.sap.com%2Firj%2Fscn%2Findex%3Frid%3D%2Flibrary%2Fuuid%2F902ce392-dfce-2d10-4ba9-b4f777843182%26overridelayout%3Dtrue&ei=AsFxT9bJNdDqrQfdoe3hDQ&usg=AFQjCNHTHvQXYtYosCLPwj98Za-LMJbo7w&cad=rja
use
\bselect\b
\b stands for word boundary. It will not match aselect or selected
look at a good regex reference at mozila.org and try your regex at regexpal
There is a very cool playground for testing regular expressions: Run Report DEMO_REGEX_TOY with SE38 or SE80.

Notepad++ Find/Replace Regex Help

I am having issues doing a string replacement in Notepad++, and need some help.
My file:
LastName,(tab)FirstName[optional]MiddleName
Some times there is data that has a middle name, sometimes not.
Public,JohnQ.
Doe,John
Clinton,WilliamJefferson
would be:
Public(tab)John(tab)Q
Doe(tab)John
Clinton(tab)William(tab)Jefferson
I want to split it out into this:
LastName(tab)FirstName(tab)MiddleName
Thanks for adding the sample input. It helps immensely to have that around. Try this and see if it does what you want.
Find, making sure Match case is checked:
([A-Z][a-z]*),([A-Z][a-z]*)(.*)
Replace with:
\1(tab)\2(tab)\3
Of course, (tab) is actually a tab character that you have to place in the replacement string yourself.
An ugly regex like this works for me on the example you've provided:
(\w+),(\w+?)(([A-Z]\w*\.?)?)\n
replace with
\1\t\2\t\3\n
Note:
This only works if the middle name starts with a letter in the A-Z. You might be able to replace [A-Z] with [[:upper:]] if notepad++ supports it (I don't know).
I need that second bracket around the middle name part because I need to match at least an empty string when there is no middle name.