how to use lookaround regex in this latex example - regex

the latex sample is as follows:
$F=K$,balalalala,balablal Bi$_x$Sb$_{1-x}$,balabla $abcd$ balabala
What I want to match is inline math expressions like $F=K$, $abcd$, while not those expressions with "_" after "$", like $_x$ and $_{1-x}$
So I write regex expression like this
\$[^_][^\$]+\$(?!_)
I add (?!_) because $Sb$ in the middle of Bi$_x$Sb$_{1-x}$ should not be considered as an math expression.
But the code is not working properly. It returns two expression
$F=K$ and $,balabla $.
What is the right regex expression for this problem?

your desired match needs a Lookbehind regex, somthing like:
\$[^$]+\$(?<!\$_[^$]+)
but we know that regex inside could not use + or * (must be fix-length), so the above regex is invalid.
I suggest to process the text in 2-pass. in first pass remove any $_xxx$ pattern :
perl -ne 's/(\$_[^\$]+\$)//g;print;'
and then match your desired pattern:
grep -oP '\$[^$]+\$'

Related

In JMeter I need to extract a specific Regular Expression

In the following String:
Events('1234', '123456', '', 'QW233Cdse');
I need to extract "QW233Cdse"
Any suggestion?
When we are working with regular expressions then its very important that we should look for the static text in the test string that can help to create a strong regular expression.
As in your case, "Events()" seems to be a static text containing dynamic value in the round parenthesis so in order to generate the regular expression you need to keep 'Events()' text and add the expression in the round parenthesis as mentioned below:
Test String: Events('1234', '123456', '', 'QW233Cdse');
Regular Expression can be:
Events(.'(.)');
Events(.* '(.+?)');
Note: The backslash before round parentheses would avoid interpreting the round braces as unescaped character. For example, a parenthesis "(" begins the definition of a quantifier, but the leading backslash of parenthesis "(" indicates that the regular expression should match the parenthesis.
Regular expression is most important item to learn when you are working with load testing tools and you can refer to below blog post to get more information on regular expression:
https://www.redline13.com/blog/2016/01/jmeter-extract-and-re-use-as-variable/
Let me know if you have any further question
The relevant regular expression would be something like:
Events\(.* '(.+?)'\);
Demo:
References:
JMeter: Regular Expressions
Using Regular Expressions in JMeter
Perl 5 Regex Cheat sheet
Try using this regex:
\w+(?='\))
Regex would be:
, '([^']+?)');
Configuration would be:

Regular expression replace double and single quotes with nothing

I am using a sphinx search module on a site I am developing and there is the option to enter regular expressions to be replaced with specified characters.
The available options are Match Expression,Replace Expression and Replace Char (these are input fields in a CMS admin panel so I'm unsure of the actual code function used behind the scenes unfortunately). My understanding is the search checks for any expressions which match Match Expression and replaces the expressions specified in Replace Expression with those specified in Replace Char. So it's a sort of find and replace on matched terms.
Some examples that work:
Example 1
Match Expression: /[a-zA-Z0-9]*-[a-zA-Z0-9]*/
Replace Expression: /-/
Replace Char: empty
Matched text: SX500-123, GLX-11A, GLZX-VXV, GLZ/123, GLZV 123, CNC-PWR1
Result text: SX500123, GLX11A, GLZXVXV, GLZ/123, GLZV-123-123, CNCPWR1
More examples here: http://mirasvit.com/doc/ssp/2.3.2/ssp/global/long_tail
What I want to do is strip any single or double quotes or apostrophes from a search query.
Example inputs: "examination papers",'examination papers,'examination' "papers",pa"pers,pa'pers
Desired outputs: examination papers,examination papers,papers,papers,papers
I have tried just replacing the - with a " in the examples listed above for now but even this hasn't worked.
Any help would be greatly appreciated! Thank you
You can use these expressions:
Match Expression - /["'][\w\s]+["']|\w+["']\w+/
This will match the following text:
"examination papers",'examination papers','examination' "papers",pa"pers,pa'pers
Then you can use this regex to replace your quotes:
Replace Expression - /["']/
Replace Char - empty
So, your output will be:
examination papers,examination papers,examination papers,papers,papers
As a context for this answer. I understand from the tool you are using that your match expression gathers a resultset where you can apply another regex expression (Replace expression) that will replace the content matched with replace char

Find and trim part of what is found using regular expression

I'm a newbie in writing regular expressions
I have a file name like this TST0101201304-123.txt and my target is to get the numbers between '-' and '.txt'
So I wrote this formula -([0-9]*)\.txt this will get me the numbers that I want, but in addition, it is retrieving the highfin '-' and the last part of the string also '.txt' so the result in the example above is '-123.txt'
So my question is:
Is there a way in regular expressions to get only part of the matched string, like a submatch of the match without the need to trim it in my shell script code for unix?
I found this answer but it is getting the same result:
Regexp: Trim parts of a string and return what ever is left
Tip: To test my regular expressions is used this website
You can use lookbehind and lookahead
(?<=-)[0-9]*(?=[.]txt)
Don't know if it would work in unix
Different regex-engines are different. Since you're using expr match, you need to make two changes:
expr match expects a regex that matches the entire string; so, you need to add .* at the beginning of yours, to cover everything before the hyphen.
expr match uses POSIX Basic Regular Expressions (BREs), which use \( and \) for grouping (and capturing) rather than merely ( and ).
But, conveniently, when you give expr match a regex that contains a capture-group, its output is the content of that capture-group; you don't need to do anything else special. So:
$ expr match TST0101201304-123.txt '.*-\([0-9]*\)\.txt'
123
sed is your friend.
echo filename | sed -e 's/-\([0-9]*\)/\1'
should get you what you want.

regular expression for find and replace

I've got strings like:
('Michael Herold','Michael Herold'),
but I need to remove the last parts so I end up with:
('Michael Herold'),
I'm still new to Regular Expressions so they confuse me. I'm using Notepad++.
find: \('([^']*)','\1'\)
Replace: ('\1')
So the actual function you use will depend on the language. Notepad++ is a text editor, not a language.
The regular expression that you will want will be ",'Michael Herold'" and you'll replace any matches with "", the empty string.
So in PHP for example, you'll have
$source = "('Michael Herold','Michael Herold')";
$pattern = "/(,'Michael Herold')+/";
$newString = $preg_replace($pattern, $source, "");
Do the equivalent in whatever language you use.
I'm not sure what flavor of regular expressions Notepad++ uses, but try replacing this expression:
\('([^']*)','\1'\)
with this one:
('$1')
The \1 matches whatever was found in the first set of single quotes (Michael Herold in your example), and $1 is replaced with that same string. (Try \1 if $1 doesn't work in Notepad++.)
See it in action here.

Regular Expression Question regarding search&replace

I'm trying to match cases with regular expression to search and replace some text of given pattern. I can match the pattern, but I'd like to keep some of the literals when replacing.
For example, from the string "abcd123," I'd like to keep abcd but remove 123. I can match the pattern using a simple regular expression like [a-zA-Z0-9]+, but when I want to replace it, I don't know what to use for the replacement. Is this even possible with just regular expressions?
Thanks a lot.
The answer depends on what language/regex engine you are using. You typically use parentheses to save sections matched and either $1, $2, ... or \1, \2, ... in the replacement string to refer to those sections.
For example, from JavaScript:
var x = "Hello World";
x.replace( /([A-Z])\w+/g, '$1xx' );
// "Hxx Wxx"
What language or text editor are you using?