I´ve had some trouble getting my RegEx find/replace working in NPP for hours. Here´s some code out of the files I´m working on:
https://regex101.com/r/kQdy4L/6/
My goal is to replace all the "0>.|.|..." by their id name
test string
<movingPart index="0>8|1|3" referencePoint="0>8|1|0|4" referenceFrame="0>" scaleZ="true"/>
bla
bla
<i3dMapping id="KroneComprimaV180XC" node="0>" />
<i3dMapping id="novoGripPart2_fixPoint" node="0>8|1|0|4" />
<i3dMapping id="novoGrip_part2" node="0>8|1|3" />
substitution
<movingPart index="novoGrip_part2" referencePoint="novoGripPart2_fixPoint" referenceFrame="KroneComprimaV180XC" scaleZ="true"/>
bla
bla
<i3dMapping id="KroneComprimaV180XC" node="0>" />
<i3dMapping id="novoGripPart2_fixPoint" node="0>8|1|0|4" />
<i3dMapping id="novoGrip_part2" node="0>8|1|3" />
After some tial´n´error I got this RegEx
(".[>].*?")|<i3dMapping id=(?P<name>".*?") node=(".[>].*?")
(".[>].*?")|<i3dMapping id=(?P<name>".*?") node=(".[>].*?")\=1
Which do find either nodes+ids or only the nodes I need to replace however I can´t figure out how to replace all "0>.|.|." with the id name
Thanks for helping me out, this is the first time I get confronted with RegEx, so this is all very confusing to me.
Cheers Fred
You can use this regexp to do the replacements for you:
(?<=(index|Point|Frame)=")([^"]+)(?=".*?id="([^"]+)" node="\2")
It uses a positive lookbehind for one of index=", Point=" or Frame=" (note we have to cut the reference off because a lookbehind must be of fixed length), followed by some number of non-" characters (that value being captured in group 2), then a positive lookahead for a string which looks like id="someidvalue" node="\2" where the \2 refers to the value captured earlier. The value someidvalue is captured in group 3.
You can then replace with $3. Note that you need to use Replace All, for some reason Notepad++ doesn't like replacing this on a match by match basis.
Here's a demo of the regex on regex101.com
Related
I have a string with a version as .v_september (every month it will vary). In this i wanted to take the value after underscore, which means "sep" (First 3 letters alone).
By using the regex .v_(.*) i am able to take the complete month and not able to get the first 3 letters alone.
Can someone help me out how can I achieve this in Apache ANT.
Thanks !
Regex functions on properties are a bit awkward in native Ant (as opposed to working with text within files). Ant-contrib has the replaceregexp task, but I try to avoid ant-contrib whenever possible.
Instead, it can be accomplished with the loadfile task and a nested filter:
<property name="version" value=".v_september" />
<loadfile property="version.month.short">
<propertyresource name="version" />
<filterchain>
<tokenfilter>
<replaceregex pattern="\.v_(.{3}).*" replace="\1" />
</tokenfilter>
</filterchain>
</loadfile>
<echo message="${version.month.short}" />
Regarding the regex pattern, note how it needs to end with .*. This is because Ant doesn't have a "match" function that simply returns the content of a capture group. It's just running a replacement, so we need to replace everything in the string that isn't part of the group.
.* will capture everything and for limiting to capturing only three characters you need to write {3} instead of *. Also you should escape the . in the beginning of your regex to only match a literal dot. You can use this regex and capture from group1,
\.v_(.{3})
Demo
My goal make html hastag, for this i'm need wrap text with # into
<a class="tag"><span class="hash">#</span>text</a>
I wan't make regexp which can give me words with # and #, but i'm have some trouble with URLs like this:
http://gitlab.com/#xxx or https://medium.com/#erikdkennedy
My example string:
<p>Some text <span class="highlighted">#test</span><br />
gitlab.com/#xxx<br />
<code>some feature</code></p>
My regexp is:
(?!.*(<mail-link|link))#([a-zA-Z0-9]+)
I get 2 matches #test and last #xxx (https://regex101.com/r/pXxIkf/1)
How i can get only test, and dont find inside the href definition?
Thank you!
Try this :
(?<=\>)(?:[\s]*(?:#|#))([a-zA-Z0-9]+)
(?<=>) Positive Lookbehind to make sure that there is > before the hashtag.
(?: start non-capturig group.
[\s]* there is whitespace or not.
(?:#|#) non-capturig group that make sure there either # or #
DEMO
I have a solution where the filename has a prefix showing the filesize of a PDF. I need to pick up that value in to a XML-file that has a lot of other info that is collected with the XSLT.
How ever I can't get just this Regex match to work.
Filename have this structure as this example:
776524_P9466_Novilon_Broschyr_SE_Omslag.xml where the digits before the underscore is the filesize.
I have a Regex search pattern of _(.*) and I can validate that it will match everything after the first section of the digits.
Here is my XSL that I'm having problems with:
<xsl:param name="find_size">
<xsl:text>(_.*)</xsl:text>
</xsl:param>
<xsl:variable name="filename_of_start"><xsl:value-of select="replace($filename_of_file, '$find_size', '')"/></xsl:variable>
<artwork_size><xsl:value-of select="$filename_of_start"/></artwork_size>
$filename_of_file has the string: 776524_P9466_Novilon_Broschyr_SE_Omslag.xml
I have also tried to match the digits before the underscore and replace with that match but haven't got that to work either. Other replaces where I remove other matches from the beginning of the string works.
Thanks
How about using the substring-before() XPath function?
<xsl:variable name="file_size" select="substring-before($filename, '_')" />
Instead of replace($filename_of_file, '$find_size', '') you want replace($filename_of_file, $find_size, '').
I am trying to replace the string with the below regex pattern but it is not getting replaced. I tried different combinations also but nothing worked. Any Idea?
<regex pattern="jre64\/1\.6\.0"
replacement="jre64/1.7.0" />
I have a problem with SLRE library, I can't figure out how to stop grabbing everything after my match. Let's say I have a html output and somewhere in the middle of buffer there is line I want to parse
name="id" value="1a2b3c4d5e6f" />
Here is my regular expression
slre_compile(&test, "name=\"id\" value=\"(.*?)\" />")
I have read about greedy and non-greedy flags in other threads where people used to have similar problem as me, but in my case adding ? to the expression doesn't change anything.
SLRE returns me match starting from 1a2b3c4d5e6f" /> and shows rest of the html page ending on </html> tag, just I don't know why. It is cutting the beginning of the html source but leaves everything after my expression. I have also tried following regex
slre_compile(&test, "^.*?name=\"id\" value=\"(.*?)\" />.*?$")
and some others, modified with greedy and non-reedy flags, which gave me same results. Does anyone know why SLRE can't stop at " /> and continues capturing characters till the source string ends?
it seems that SLRE does not understand non-greedy qualifiers and parses .*? instead as if it were (?:.*)?. However, in this case \"[^\"]*\" should work...