Typoscript - Use regex replace on field value - regex

A part of a COA I have gets the page title. What I would like to do is replace the space between the last two words with a
But adding the stdWrap.replacement part results in no output at all:
stdWrap.cObject = COA
stdWrap.cObject {
10 = TEXT
10.field = title
10.htmlSpecialChars = 1
10.wrap = <h2>|</h2>
10.stdWrap.replacement {
10 {
search = \s(\S+)$
replace = \1
useRegExp = 1
}
}
}

Have a look at the Typo3 replacement docs:
10 {
search = #(a )CAT#i
replace = \1cat
useRegExp = 1
}
The # in #(a )CAT#i are regex delimiters. So, all you need is provide any, say:
search = /\s(\S+)$/

Related

Groovy - How to substitute elements in a string that were found with a regex

I'm trying to create a generic solution for increasing a version number which regex is given as a parameter. The regex works well so far but the next part, which is returning the same syntax as the original one, is what drives me crazy right now.
The code snippet:
def increase(tag, regex, position){
tag[position] = (tag[position] as Integer) + 1
def newTag = tag[0].replaceAll(regex, ???)
return newTag
}
So, if I give these parameters:
// entry params:
tag = ['1.0.RC119', 1, 0, 119 ]
regex = /^(\d+).(\d+).RC(\d+)/
position = 3
The result should be 1.0.RC120
With these parameters:
tag = [ '1.0u2', 1, 0, 2 ]
regex = /^(\d+).(\d+)u(\d+)/
position = 3
The result should be 1.0u3
What should I give to replaceAll as replacement?
You may split the string with 1+ digits and then interleave the resulting list with the tag items and joining to get the final string:
List combineList(List one, List two) {
def result = [one, two].transpose()
( result += (one - result*.get(0)) ?: (two - result*.get(1)) ).flatten()
}
def increase(tag, regex, position){
tag[position] = (tag[position] as Integer) + 1
return combineList(tag[0].split(/\d+/), tag[1..-1]).join())
}
See a Groovy demo online.
I figured out, though I think it's a little "dirty":
def increase(tag, regex, position){
tag[position] = tag[position] + 1
def newTag = regex.toString().\
replace('^', '').\
replace('\$', '').\
replace('(d+)','#__#')
tag[1..-1].each {
newTag = newTag.replaceFirst('#__#', it.toString())
}
return newTag
}
Surely it won't cover all the cases, but will mostly work for me.

Search for multiple string occurrence in String via PHP

I am working on an ecommerce website using MVC,php. I have a field called description. The user can enter multiple product id's in the description field.
For example {productID = 34}, {productID = 58}
I am trying to get all product ID's from this field. Just the product ID.
How do i go about this?
This solution doesn't use a capture group. Rather, it uses \K so that the full string elements become what would otherwise be captured using parentheses. This is a good practice because it reduces the array element count by 50%.
$description="{productID = 34}, {productID = 58}";
if(preg_match_all('/productID = \K\d+/',$description,$ids)){
var_export($ids[0]);
}
// output: array(0=>'34',1=>'58')
// \K in the regex means: keep text from this point
Without using regex, something like this should work for returning the string positions:
<code>
$html = "dddasdfdddasdffff";
$needle = "asdf";
$lastPos = 0;
$positions = array();
while (($lastPos = strpos($html, $needle, $lastPos))!== false) {
$positions[] = $lastPos;
$lastPos = $lastPos + strlen($needle);
}
// Displays 3 and 10
foreach ($positions as $value) {
echo $value ."<br />";
}
</code>

Notepad++ How to replace everything in between certain lines

So I have a code like this
TitleManager:AddSubTitleMissionInfo_LUA({
m_iID = 10,
m_wstrDescription = "Professional Killer",
m_eClearType = TITLE_MISSION_CLEAR_TYPE.TMCT_MOB_KILL_COUNT,
m_bAutomaticDescription = True,
m_ClearCondition = {
m_eMobID = {68},
m_iMobKillCount = {1}
}
})
TitleManager:AddSubTitleMissionInfo_LUA({
m_iID = 20,
m_wstrDescription = "Sneaky Assassin",
m_eClearType = TITLE_MISSION_CLEAR_TYPE.TMCT_MOB_KILL_COUNT,
m_bAutomaticDescription = True,
m_ClearCondition = {
m_eMobID = {69},
m_iMobKillCount = {1}
}
})
TitleManager:AddSubTitleMissionInfo_LUA({
m_iID = 20,
m_wstrDescription = "Merciless Thug",
m_eClearType = TITLE_MISSION_CLEAR_TYPE.TMCT_MOB_KILL_COUNT,
m_bAutomaticDescription = True,
m_ClearCondition = {
m_eMobID = {70,71},
m_iMobKillCount = {1,1}
}
})
There are like a hundred of those, all different.
How do I replace everything in between the curly brackets.
m_ClearCondition = {
}
to
m_ClearCondition = {
m_eMobID = {50},
m_iMobKillCount = {1}
}
I really hope someone could answer my question, I would be really grateful.
Description
There are several unclear things about your sample text from your post. But this expression assumes:
each m_clearcondition value will have no nested brackets more then one level deep.
will find each key name m_clearcondition and place that into capture group 1
the close bracket will be captured into group 2
Regex:
^(\s+m_ClearCondition\s*=\s*\{)(?:\{[^}]*\}|[^}])*(\})
Replace with: $1\n m_eMobID = {50},\n m_iMobKillCount = {1}\n $2
Example:
Live demo of the regex: http://regexr.com?35n0l
In this example I'm using Notepad++ 6.4.2. There where known problems using regex in Notepad version 5 and lower.
Regex is your friend. Ctrl-H (Find/replace) and enable regular expressions (bottom left of dialog).
\{\s*\}$
replace with:
{\nwhatever you want\n}\n
Regex Match Explanation:
{} are reserved, so escaping them will match the actual brace character
\s of course will match all whitespace (including new lines and tabs)
$ would be the end of the line; this just helps the match
\n in the replace would insert new lines where it makes sense; these are optional.

Java Matcher Error

My string:
null[00:14.04]I've /n[00:14.11]got /n[00:14.18]a /n[00:14.25]fee- /n[00:15.02]ling /n
I am trying to obtain every data between [<--->] brackets. Here's my code.
String find = "[(.*?)\\\\]";
Pattern patern = Pattern.compile(find);
Matcher matcher = patern.matcher(intake);
while(matcher.find()){
i++;
matcher.find(i);
int start = matcher.start();
int end = matcher.end();
String group = matcher.group();
}
The first results are:
start = 10
end = 11
group = "."
What I wanted was (Counting on my head)
start = 4
end = 14
group = [00:14.04]
Next is
start = 22
end = 32
group = [00:14.11]
and so on
What is the correct pattern?
You're using wrong escaping. Use this regex:
String find = "\\[(.*?)\\]";
EDIT: Based on your comment:
If you want to capture all items inside square brackets just run your while loop like this:
while(matcher.find()) {
String matched = matcher.group(1);
System.out.printf("Matched Group: [%s]%n", matched);
}

How to highlight a string within a string ignoring whitespace and non alphanumeric chars?

What is the best way to produce a highlighted string found within another string?
I want to ignore all character that are not alphanumeric but retain them in the final output.
So for example a search for 'PC3000' in the following 3 strings would give the following results:
ZxPc 3000L = Zx<font color='red'>Pc 3000</font>L
ZXP-C300-0Y = ZX<font color='red'>P-C300-0</font>Y
Pc3 000 = <font color='red'>Pc3 000</font>
I have the following code but the only way i can highlight the search within the result is to remove all the whitespace and non alphanumeric characters and then set both strings to lowercase. I'm stuck!
public string Highlight(string Search_Str, string InputTxt)
{
// Setup the regular expression and add the Or operator.
Regex RegExp = new Regex(Search_Str.Replace(" ", "|").Trim(), RegexOptions.IgnoreCase);
// Highlight keywords by calling the delegate each time a keyword is found.
string Lightup = RegExp.Replace(InputTxt, new MatchEvaluator(ReplaceKeyWords));
if (Lightup == InputTxt)
{
Regex RegExp2 = new Regex(Search_Str.Replace(" ", "|").Trim(), RegexOptions.IgnoreCase);
RegExp2.Replace(" ", "");
Lightup = RegExp2.Replace(InputTxt.Replace(" ", ""), new MatchEvaluator(ReplaceKeyWords));
int Found = Lightup.IndexOf("<font color='red'>");
if (Found == -1)
{
Lightup = InputTxt;
}
}
RegExp = null;
return Lightup;
}
public string ReplaceKeyWords(Match m)
{
return "<font color='red'>" + m.Value + "</font>";
}
Thanks guys!
Alter your search string by inserting an optional non-alphanumeric character class ([^a-z0-9]?) between each character. Instead of PC3000 use
P[^a-z0-9]?C[^a-z0-9]?3[^a-z0-9]?0[^a-z0-9]?0[^a-z0-9]?0
This matches Pc 3000, P-C300-0 and Pc3 000.
One way to do this would be to create a version of the input string that only contains alphanumerics and a lookup array that maps character positions from the new string to the original input. Then search the alphanumeric-only version for the keyword(s) and use the lookup to map the match positions back to the original input string.
Pseudo-code for building the lookup array:
cleanInput = "";
lookup = [];
lookupIndex = 0;
for ( index = 0; index < input.length; index++ ) {
if ( isAlphaNumeric(input[index]) {
cleanInput += input[index];
lookup[lookupIndex] = index;
lookupIndex++;
}
}