replace second occurrence of comma with line break in string

Trying to figure out what’s the best way to insert a line break after the second comma in a string. Here’s what I’m currently using and it works, but I would like to know if a regex would be better for this.

var data = $('#test1').html();
var position = data.indexOf(",", data.indexOf(",") + 1);

String.prototype.replaceAt = function(index, replacement) {
  return this.substr(0, index) + replacement + this.substr(index + replacement.length);

var newstr = (data.replaceAt(position, "<br />"));

<script src=""></script>
<span id="test1">3131, abc street, ZZ, ZZ  128 376</span>
<br /><br />
<span id="result">###</span>

$1 in Regular Expression is returning empty in Java

I have the following Java code

Pattern p = Pattern.compile(KeyWordsRegExp);
Matcher m = p.matcher(str);
StringBuffer sb = new StringBuffer();
while (m.find()) {
    m.appendReplacement(sb, "<span class=\"colorForKeyword\">$1</span>");

I am trying to locate keywords in a string and replace them with some HTML markup. I would like

public void test{} 

to return as

<span class="colorForKeyword">public</span> <span class="colorForKeyword">void</span> test{}

The java code I have above locates the words fine but the $1 expression is not replacing the found pattern so I get

<span class="colorForKeyword"></span> <span class="colorForKeyword"></span> test{}

KeyWordRegExp is

private String KeyWordsRegExp = "(\\babstract\\b)|(\\bcontinue\\b)|(\\bfor\\b)|(\\bnew\\b)|(\\bswitch\\b)|(\\bassert\\b)|(\\bdefault\\b)|(\\bgoto\\b)|(\\bpackage\\b)|(\\bsynchronized\\b)|(\\bboolean\\b)|(\\bdo\\b)|(\\bif\\b)|(\\bprivate\\b)|(\\bthis\\b)|(\\bbreak\\b)|(\\bdouble\\b)|(\\bimplements\\b)|(\\bprotected\\b)|(\\bthrow\\b) |(\\bbyte\\b)|(\\belse\\b)|(\\bimport\\b)|(\\bpublic\\b)|(\\bthrows\\b)|(\\bcase\\b)|(\\benum\\b)|(\\binstanceof\\b)|(\\breturn\\b)|(\\btransient\\b)|(\\bcatch\\b)|(\\bextends\\b)|(\\bint\\b)|(\\bshort\\b)|(\\btry\\b)|(\\bchar\\b)|(\\bfinal\\b)|(\\binterface\\b)|(\\bstatic\\b)|(\\bvoid\\b) |(\\bclass\\b)|(\\bfinally\\b)|(\\blong\\b)|(\\bstrictfp\\b)|(\\bvolatile\\b)|(\\bconst\\b)|(\\bfloat\\b)|(\\bnative\\b)|(\\bsuper\\b)|(\\bwhile\\b)";

str is what I am pattern matching against, thus in the example I provide “public void test{}

PHP remove last empty line in a textfile

I have a problem with a textfile.
I have a textfile with an empty line (always the last line is empty).
I need to remove this line. I tried several ways. Here is my current way to delete empty lines:

function RemoveEmptyLines($filename)
    $myfile = fopen($filename, "r") or die("Unable to open file!");
    $Content = fread($myfile,filesize($filename));
    $NewContent = preg_replace('/^\s+/m', '', $Content);
    $myfile2 = fopen('new.txt', "w") or die("Unable to open file!");
    fwrite($myfile2, $NewContent);
    echo "removed";

This deletes all the empty lines within the textfile, but not the last empty line.
If the content is:

1 \n 2 \n \n 3

It deletes the empty line.
If it is:

1 \n 2 \n 3 \n \n

It doesn´t..
Any solutions?
The problem is, that the file is dynamic, so I can´t just delete the last line, because it´s not always empty..

Regex Help for Tokenisation

Trying to put together a Tokeniser within a python application, need to use a regexp to do so.

Could someone guide me on Syntax to match the following criteria?
– Must start with a letter (lower or caps)
– Can have numbers after it
– Must be at least 2 letters in length

I know it’s not too complex, but I just don’t know much about regular expressions (yet).

Thanks stack!

Javascript regex pattern match multiple strings ( AND, OR, NEAR/n, P/n )

I need to filter a collection of strings based on a rather complex query
I have query input as a string

var query ='Abbott near/10 (assay* OR test* ) AND BLOOD near/10 (Point P/1 Care)'; 

From this query INPUT string I want to collect just the important words:

var words= 'Abbott assay* test* BLOOD Point care';

The query can change for example:

var querry2='(assay* OR test* OR analy* OR array) OR (Abbott p/1 Point P/1 Care)';

from this query need to collect

var words='assay* test* analy* array Abbott Point Care';

I’m looking for your suggestion.


Perl Regex match delimited string

For the life of me I can’t figure this out in Perl syntax. I’ve manged to get this flavour working in Javascript where N is the match number.


I’ve also tried matching against anything that isn’t a pipe, however I just can’t seem to write the regex to extract the match index in the Perl flavour that I need. Any pointers?

Data looks like this

astring| another string|test|maybe some more text

When the first regex runs in the regex debugger it produces this (which is what I’d like to run) .

^(?:[^\|]*\|){0}([^\|]*) produces 'astring'
^(?:[^\|]*\|){3}([^\|]*) produces 'maybe some more text'

However, when I run it in Netezza (Perl Regex, this is where I need it to run) it produces:

^(?:[^\|]*\|){0}([^\|]*) produces 'astring'
^(?:[^\|]*\|){3}([^\|]*) produces 'astring| another string|test|maybe some more text'

Problem solved the Regex was okay, however was me being a dunce and not using the function in Netezza correctly. This led to a huge over complication of the regex. Example below REGEXP_EXTRACT(column, ‘[^\|]+’,1 ,4 )

RegEx: REPLACE REGEX to remove first character and everything after the last _ [duplicate]

This question already has an answer here:

I have this RegEx which Extracts word after removing the first letter a and everything after the last underscore “_”. I need to a same output but for “Replace” RegEx.

Input: asome test file-2_rev2.docx
RegEx: (?<=^a).+?(?=_.+?$)
Result: some test file-2

The code above works but an Extract. I need to be able to use a REPLACE RegEx. Basically the extract Regex put the result into a collection variable and I have to 5 more lines of code to get the value. The replace RegEx must be one liner like I posted. Any suggestion?

I tried below but i need to combine this into one


This (^.*?(?=a)a)|[^_]+$ gives me the file name but not removing the last underscore

How to add the pattern regexp in HTML for the following condition?

I’m still actively trying to understand how regexp works but I need to work a regexp based on the following conditions:

  1. Only allow the length of the input to be exactly 7
  2. The first 4 can only be letter
  3. The last 3 can only be numbers from 0 to 9
  4. The 4th of the word can be a space if there are only 3 letters given and numbers were then followed.

P/S: I’m not a developer so I may have a hard time understand the logic.

Regular expressions: how to use ‘or’ without more capturing groups?

We are supposed to only add the regular expression to a script without touching anything else. The script is made so that it accepts 3 capturing groups and there are 2 different regular expressions on which depends the output. I prepared the epxressions for both cases, but when I do:

expression1 | expression2

The script thinks that there are 6 capturing groups (each expression containing 3 capturing groups) and therefore gives me an error.

How should I join the 2 expressions without the script thinking that there are 6 capturing groups, because both expressions can never be applied, it is either expression1 or expression2, but never both at the same time.

The expressions1 is:

([a-zA-Z0-9]+[ ]?)\(P[: ]?([a-zA-Z0-9 ]*)()

The expressions2 is:

()()\)[ ]+([a-zA-Z0-9]+$|[a-zA-Z0-9]+[ ])

Thank you for any suggestions!