I have a string like below.
update comment for line OBC-1234:Message is this
I wanted to match OBC-1234:Message is this out of the above string.
Regex I used is \w*-\d+:(\w+\s?)+
The tool on which I work have only one function which replaces matched regex by some input parameter.
That means it will first match the regex from the string and will replace it by given input.
But my requirement is to replace the unmatched string by the given input.
The output should be like below
update comment for line input
I know it can be done through negation but I don't how to use it for a bigger string. Please help.
You can do:
/(.*?)(?:[A-Z\d-]+:[\w ]+)$/\1New Addition/
^ words and ' ' to end of line
^ literal :
^ character class for OBC-1234 pattern
^ Non capturing group
^ Capture to the LH of description
Demo
If the OBC-1234 is more concrete, you can do:
/(.*?)(?:[A-Z]+-\d+:[\w ]+)$/\1New Addition/
to be more specific.
Demo 2
Use:
Find: ^(.*?)\w*-\d+:\w+(?:\s+\w+)*
Replace: $1NEW STRING
Related
Lets say this is my teststring:
XXX 3.14 QQQ
XXX 3.14 QQQ
YYY
I would like to have the second 3.14 as the exact match. I think this would require me to combine the next two code lines:
((?<=XXX ).*(?= QQQ)) which selects both 3.14.
.*\n(?=((.*\n){1})(YYY)) which selects the full second line.
However, when I use ((?<=XXX ).*(?= QQQ)).*\n(?=((.*\n){1})(YYY)) the exact match is the second "3.14" and "QQQ".
Any help would with finding out how to match the second 3.14 with the use of these formulas would very much be appreciated.
Thank you
If this is really your string and not just a part of it, you could make use of \A, the very beginning of a string:
^(?<!\A)XXX(.+?)QQQ$
See a demo on regex101.com.
It might be easier to specify your programming language and to use the corresponding array location in the found matches (e.g. results[1]).
If you want to match the second number, you could also match the exact content instead of using lookarounds and use a single capturing group for the second value.
^XXX \d+\.\d+ QQQ\r?\nXXX (\d+\.\d+) QQQ\r?\nYYY\b
^ Start of string
XXX \d+\.\d+ QQQ match the first line
\r?\n Match a newline
XXX (\d+\.\d+) QQQ Match the second line and capture the number in a capture group
\r?\nYYY\b Match a newline, YYY and word boundary
Regex demo
You where on the correct path, the following should work:
(?<=^XXX )[^\n]*(?= QQQ$)(?!.*^XXX [^\n]* QQQ$)
Which says match everything (unless a newline) between <start of line>XXX and QQQ<end of line>, if not followed by another sequence of ^XXX [^\n]* QQQ$.
For the above regex to function appropriately you'll need to set the multiline flag (m) to let ^ and $ match the beginning and ending of a line, rather than the string. You also need to set the single line/dot all flag (s).
If you don't care about the XXX being adjacent to the start of the line and QQQ being adjacent to the end of the line you can leave out the ^ and $ anchors and don't have to set the multiline flag. This version would look like:
(?<=XXX )[^\n]*?(?= QQQ)(?!.*XXX [^\n]* QQQ)
My regex already works well but I would like to remove the " character at the output. Is this possible with Regex?
Regex: (?>\".*?\")
Link: https://regex101.com/r/G7OQ0a/2/
"SharedKeys" = "0","1","2","3","4","5","6","7","8","9"
"BroadCastKeys" = "0","1","2","3","4","5","6","7","8","9"
"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"
"ProgramPath" = "D:\Games\WoW\World of Warcraft\Wow.exe"
Match: "BroadCastKeys" or "L" and so on
My target: BroadCastKeys or L and so on
You can do it with this pattern:
(?!\G)"\K[^"]*
demo
The idea it to skip the position of the closing quote (without consuming it with the pattern). To do that (?!\G) forbids the matches to be consecutive. (\G matches the position of the last successful match or the start of the string).
Note that if your string may start with a double quote, you need to change the pattern to (?!\G(?!\A))"\K[^"]* to allow the first match.
You can also make it more simple and use a capture group:
"([^"]*)"
im kinda strumped in a situation where i need to match a whole string with a regular expression rather than finding if the pattern exists in the string.
suppose if i have a regular expression
/\\^^\\w+\\$^/
what i want is that the code will run through various strings , compare the strings with the regular expression and perform some task if the strings start and end with a ^.
Examples
^hello world^ is a match
my ^hello world^ should not be a match
the php function preg_match matches both of the results
any clues ???
Anchor the ends.
/^...$/
Here is a way to do the job:
$strs = array('^hello world^', 'my ^hello world^');
foreach($strs as $str) {
echo $str, preg_match('/^\^.*\^$/', $str) ? "\tmatch\n" : "\tdoesn't match\n";
}
Output:
^hello world^ match
my ^hello world^ doesn't match
Actually, ^\^\w+\^$ will not match "^hello world^" because you have two words there; the regex is only looking for a single word enclosed by "^"s.
What you are looking for is: ^\^.*\^$
This will match "^^", "^hello world^", "^a very long string of characters^", etc. while not matching "hello ^world^".
You can use the regex:
^\^[\w\s]+\^$
^ is a regex meta-character which is used as start anchor. To match a literal ^ you need to escape it as \^.
So we have:
^ : Start anchor
\^: A literal ^
[\w\s]+ : space separated words.
\^: A literal ^
$ : End anchor.
Ideone Link
Another pattern is: ^\^[^\^]*\^$ if you want match "^hello world^" and not "hello ^world^" , while \^[^\^]*\^ if you want match "^hello world^" and world in the "hello ^world^" string.
For Will: ^\^.*\^$ this match also "^hello^wo^rld^" i think isn't correct.
Try
/^\^\s*(\w+\s*)+\^$/
The regex I'm searching has the following constraints:
it starts with "//"
then "[" a non number sequence (called delimiter in this list) and "]"
next line "\n"
"[" 0 or more number separated by the delimiter previously found "]".
For example the following text matches the regex:
//[*#*]
[1*#*34*#*64]
and the following text doesn't match the regex:
//[*#*]
[1#34#64]
because the delimiter is not the same matched in the first row
The regex I currently create is
^//\[(\D)+\]\n\[[(\d)+(\D)+]*(\d)+\]$|^//\[(\D)+\]\n\[\]$|^//\[(\D)+\]\n\[(\d)+\]$
but obviously this regex match with both previous examples.
Is there a way to "recall" a char sequence already matched in the regex itself?
You need something called back-reference (a very good tutorial here).
Use this regex in Python:
r'^//\[([^\]]+)\]\n\[\d+(\1\d+)*\]'
Sample run:
>>> string = """//[*#*]
... [1*#*34*#*64]"""
>>> print re.search(r'^//\[([^\]]+)\]\n\[\d+(\1\d+)*\]',string).group(0)
//[*#*]
[1*#*34*#*64]
will match your string in Python.
Debuggex Demo
You need to use a back-reference, in most languages you can reference a matching group using \n where n is the group number.
This pattern will work:
//\[([^]]++)]\n\[(?>\d++\1?)+]
To break it down:
// just matches the literal
\[([^]]++)] matches some characters in square brackets
\n matches the newline
\[(?:\d++\1?)++] matches one or more digits followed by the match captured in the first pattern section - optionally. This is an atomic group.
I'm so close to understanding regex. I'm a bit stumped, I thought i understood lazy and greedy.
Here is my current regex: <g_n><!\[CDATA\[([^]]+)(?=]]><\/g_n>)
My current regex makes:
<g_n><![CDATA[xxxxxxxxxx]]></g_n>
match to:
<g_n><![CDATA[xxxxxxxxxx
But I want to make it match like this:
xxxxxxxxxx
You want
<g_n><!\[CDATA\[(.*?)]]></g_n>
then if you want to replace it use
\1
in the replacement box
Your matching the whole string, the brackets around the .*? match all of that and put it in the \1 variable
So the match will be all of the string with \1 referring to what you want
To change the xxxxx
Regex :
(<g_n><![CDATA[)(?:.*?)(]]></g_n>)
Replacement
\1WHAT YOU WANT TO CHANGE TO\2
It looks like you need to add escape slashes to the two closing square brackets, as they are literals from the string you're parsing.
<g_n><!\[CDATA\[.*+?\]\]><\/g_n>
^ ^
Any square brackets not being escaped by backslashes will be treated as regex operational brackets, which in this case won't catch the input string.
EDIT, I think the +? is redundant.
\[.*\]\]> ...
should suffice, since .* means any character, any amount of times.
Tested with notepad++ 6.3.2:
find: (<g_n><!\[CDATA\[)([^]]+)(?=]]></g_n>)
replace: $1WhatYouWant
You can replace + by * in the pattern to match void CDATA:
<g_n><![CDATA[]]></g_n>