How match a regex multiline in java? - regex

I have this string with a new line in it
And it does not match anything in a new line "W https://stadt.muenchen.de/infos/portrait-referat- bildung-sport.html"
This is my regex \bW\s((www\.\s*|https:\/\/)?[\w-]+(\.[\w-]+)+(\/[\w\-.\/?%&=]*)?)
https://regex101.com/r/KEACmP/1
I want that the full link is matching. So that the regex is multiline and stops there then
So I want "W https://stadt.muenchen.de/infos/portrait-referatbildung-sport.html"

in addfition to your regex i have added \s*.*?\.html
\s* will match new line or any spaces
\bW\s((www\.\s*|https:\/\/)?[\w-]+(\.[\w-]+)+(\/[\w\-.\/?%&=]*)?)\s*.*?\.html

Related

Regex, stop before a backslash caracter?

i want to use regex for replace a string in a file in powershell, this is the regex:
=.*\\app\\client\\.*\\
I applied this regex on that string:
HOME= C:\app\client\Administrateur\product
And i want this result:
= C:\app\client\Administrateur
But I have this result:
= C:\app\client\Administrateur\
How to say to regex i want to stop before the next backslash ?
Your pattern =.*\\app\\client\\.*\\ will match the last occurrence of \app\client\ and will then match until the last occurrence of the forward slash.
To match what comes after app\client\ but not include the last backslash you could use a negated character class matching not a backslash:
=.*\\app\\client\\[^\\]*
Regex demo
If the .* part at the start can not contain a backslash, this would be another option to prevent needless backtracking because the .* would first match until the end of the string:
=[^\\]*\\app\\client\\[^\\]*
Regex demo

Eclipse Add text to first line of all files

I need to add text to first line of all my JSP's in eclipse, this is the regex I a using \A.* but some how it selects the first line, I just want to prepend text to the start of the file. any help will be very much appreciated.
The .* pattern matches any 0+ chars other than line break characters, so it matches the first line.
It seems that Eclipse Find/Replace regex feature does not match entirely zero-width patterns (e.g. (?=,) will not find and insert a text before commas).
A workaround is to match and capture some text with (...) (where ... stand for a consuming pattern) capturing group and use $1 in the replacement pattern to reinsert the matched text.
Use
\A(.*)
Replace with MY_NEW_TEXT_HERE_AT_THE_START_OF_FILE$1.

Cut lines using Notepad++ Regexp replace

I need to cut lines that have 6 or more characters, hyphen, then other characters or symbols. Hyphen and rest of line should be removed. Source text:
0402CS-2
0402CS-3
0402
7812-C
0603CS-1
0603CS-2
0603CS-3
As a result, I need this:
0402CS
0402CS
0402
7812-C
0603CS
0603CS
0603CS
To do that, I use Notepad++ regexp replace feature. Find pattern: ^([^\-]{6,})\-.+$ Replace pattern: \1
But there is no option "multiline", so, symbols "^" and "$" doesn't match ONLY beginning and end of the line and actually I have result:
0402CS
0402CS
0402
7812 <-- that's wrong!
0603CS
0603CS
0603CS
Please advice me how to fix find pattern? Or, maybe there is other handful and powerful free text editor that can do that?
^([^\n\-]{6,})\-.+$
^^
Just use \n as due to [^-] the regex can traverse to line below as use that line to make a match.
See demo.
https://regex101.com/r/BHO93c/1
for the input
0402
7812-C the regex matches both lines as 1 line and makes a match.
See demo if 0402 is not there.
https://regex101.com/r/BHO93c/2
That happens because the [^-] character class also matches a newline.
Add \n to it:
^([^\n-]{6,})-.+$
See the regex online demo (note the m multiline modifier (making ^ match the start of the line, and $ - the end of the line) and g modifier (enabling search for multiple occurrences) that is ON by default in Notepad++).
Note that escaping the hyphen is not necessary inside a character class when it is at the start/end of the class, and you never need to escape the hyphen outside the character class.

regular expression in sublime text 2 to match text

I have a string that looks like this:
lonfksa.newsvine.com
and I have tons of file that looks like this:
http://ricambi.ru/avtomobilnie-novosti/lexus-gotovit-k-debiutu-obnovlenniy-rx
http://www.kiwibox.com/hoytboar/blog/entry/121424391/modis-tshirt-tips-untuk-womens-clothing/
http://www.euro-rockradio.com/archives/category/interview
http://lonfksa.newsvine.com/_news/2014/04/18/23538711-vampir-romantis-clothing
http://www.fam-hinterseer.de/cgi-bin/info.php?a%5B%5D=%3Ca+href%3Dhttp%3A%2F%2Fwww.shopious.com%3Ecart+means+payment%3C%2Fa%3E
http://www.kiwibox.com/donniehihp/blog/entry/116146741/skin-care-beauty-makeup-tips-for-female/
http://www.kiwibox.com/karlagbr/blog/archive/2014/9/7/
I wanted to match the line that contains:
lonfksa.newsvine.com
and I tried the following regex but it doesn't work:
(?s)lonfksa.newsvine.com(?s)
what regex should I use to match the whole line that has this string?
You can make use of the multiline flag, and ^ and $ anchors that will match at the string start and string end repsectively:
(?m)^.*lonfksa\.newsvine\.com.*$
Mind that you need to escape a dot in regex to make it match a literal dot. Your regex (?s)lonfksa.newsvine.com(?s) contains unescaped dots that match any character (even a newline since you are using a singleline inline option (?s)). The final inline option (?s) is not necessary, it does not do anything.
Try this regex :
^.*lonfksa\.newsvine\.com.*\b
Demo

Match line only if next line is an empty Line

I'm very new to regex, what I'm trying to do is to match a line only if the next line is an empty line.
For example:
This is some text
( empty line )
This is some text
This is some text
This is some text
( empty line )
This is some text
( empty line )
In the previous example, I would like to be able to select only line 1,5,7.
Is this possible with regex in notepad++?
Thanks in advance.
You can use this regex,
(.*)\n\s*\n
and replace with
\1
Working Demo
It uses a concept of group capture, so here you can use \1 to use captured group, that is line before newline
You could try the below positive lookahead based regex.
^.*?\S.*(?=\n[ \t]*$)
\S matches any non-space character. So .*?\S.* matches the line which has at-least one non-space character and the following (?=\n[ \t]*$) asserts that the match must be followed by a newline character and then followed by zero or more space or tab characters.
OR
^.*?\S.*(?=\n\n)
If you mean empty line as line which don't have any single space or tab characters, then you could use the above regex. (?=\n\n) asserts that the match must be followed by a blank line.
DEMO 1
DEMO 2
This should do the trick:
/(.)*\n\n/
If you're looking for an easy way to test / verify regex rubular is pretty good.