I am trying to match the below string with Regular expression
string : PKGx.1234 ... BBA
Regular expression : ^\bPKG[0-9]{0,1}.[0-9]{0,4}\ ...\ \bBB[A-B]{1}?$
but i am getting no match error
can anyone help me with how can i remodify the regular expression to match the given string ..?
You have a character x after PKG that the pattern tries to match with an optional digit [0-9]? If there should be an optional single a lowercase character, you can use [a-z]? instead.
You can omit the word boundary before the BB as there is an implicit word boundary in between.
Note that you don't have escape the spaces, but you do have to escape the dot to match it literally.
^PKG[a-z]?\.[0-9]{0,4} \.{3} BB[A-B]\b
Regex demo
If you want to match the whole string including the space and comma at the end, including using the $ anchor to assert the end of the string:
^PKG[a-z]?\.[0-9]{0,4} \.{3} BB[A-B] , *$
Regex demo
Related
I'm looking for a regular expression that matches strings for a syntax highlighter used in a code editor. I've found
(")(?:(?!\1|\\).|\\.)*\1
from here regex-grabbing-values-between-quotation-marks (I've changed the beginning since I only need double quotes, no single quotes)
The above regular expression correctly matches the following example having escaped double quotes and escaped backslashes
"this is \" just a test\\"
Most code editors however also highlight open ended strings such as the following example
"this must \" match\\" this text must not be matched "this text must be matched as well
Is it possible to alter the above regular expression to also match the open ended string? Another possibility would be a second regular expression that just matches the open ended string such as
"[^"]*$ but match only if preceded by an even count of non-escaped quotes
You could use an alternation to match either a backreference to group 1 or assert the end of the string with your current pattern.
(")(?:(?!\1|\\).|\\.)*(?:\1|$)
But as you are only capturing a single character (") you can omit the capture group and instead of the backreference \1 just match "
Alternatively written pattern:
"[^"\\]*(?:\\.[^"\\]*)*(?:"|$)
See a regex demo.
If the match should not start with \" and a lookbehind is supported:
(?<!\\)"[^"\\]*(?:\\.[^"\\]*)*(?:"|$)
This pattern matches:
(?<!\\) Negative lookbehind, assert not \ directly to the left
" Match the double quote
[^"\\]* Optionally match any char except " or \
(?:\\.[^"\\]*)* Optionally repeat matching \ and any char followed by any char except " or \
(?:"|$) Match either " or assert the end of the string.
i want to use regex for replace a string in a file in powershell, this is the regex:
=.*\\app\\client\\.*\\
I applied this regex on that string:
HOME= C:\app\client\Administrateur\product
And i want this result:
= C:\app\client\Administrateur
But I have this result:
= C:\app\client\Administrateur\
How to say to regex i want to stop before the next backslash ?
Your pattern =.*\\app\\client\\.*\\ will match the last occurrence of \app\client\ and will then match until the last occurrence of the forward slash.
To match what comes after app\client\ but not include the last backslash you could use a negated character class matching not a backslash:
=.*\\app\\client\\[^\\]*
Regex demo
If the .* part at the start can not contain a backslash, this would be another option to prevent needless backtracking because the .* would first match until the end of the string:
=[^\\]*\\app\\client\\[^\\]*
Regex demo
I want to replace
$this->input->post("product_name");
with
$post_data["product_name"];
I want to use notepad++ regex, but I couldn't find proper solution
In find --> $this->input->post("[\*w\]");
In replace --> $post_data["$1"];
but its not working
The $this->input->post("[\*w\]"); pattern does not work because:
$ is a special char matching the end of a line, you need to use \$ to match it as a literal char
[\*w'\] is a malformed pattern as there is no matching unescaped ] for the [ that opens a character class. Also, w just matches w, not any letter, digit or underscore, \w does that.
You may use
Find What: \$this->input->post\("(\w*)"\);
Replace With: $post_data["$1"];
If there can be any char inside double quotes use .*? instead of \w*:
Find What: \$this->input->post\("(.*?)"\);
Regulex graph:
NPP test:
Use this pattern to match desired text \$this->input->post\(("[^"]+")\);
And replace it with pattern \$post_data\[\1\]
Explanation:
\$this->input->post - matach $this->input->post literally
\(("[^"]+")\); - match (literally, then match double quates and everything between them with "[^"]+" and store inside first capturing group, then match ); literally
To replace
$this->input->post("product_name");
by
$post_data["product_name"];
do replace, with regex activated
this->input->post\("(.*)"\);
by
post_data\["\1"\];
The \x with x a number, corresponds to the x-th match catched with the parenthesis. Here we catch any character inside this->input->post(XXXX);
Don't forget to escape special character with \.
Your special characters were []()
I need to write a regex to allow the contents but block the special characters ' and -- in the string. I am working on a product which uses the regex to allow or block contents goofing around the product, I managed to find the below pattern:
^('|--|#|\\x27|\\x23)$
Which is supposed to match --, ' and # in the string, but when I tested this pattern in some online regex pattern matching. it was not highlighting the string when it contains --, ' or #.
See Start of String and End of String Anchors at regular-expressions.info:
The caret ^ matches the position before the first character in the string. Applying ^a to abc matches a. ^b does not match abc at all, because the b cannot be matched right after the start of the string, matched by ^.
Similarly, $ matches right after the last character in the string. c$ matches c in abc, while a$ does not match at all.
Also, \x27 matches a ', and \x23 matches a #, thus, no need doubling them with literals.
So, you just need
(--|\x27|\x23)
Or (using a non-capturing group):
(?:--|\x27|\x23)
See demo
I am using Notepad++ to remove some unwanted strings from the end of a pattern and this for the life of me has got me.
I have the following sets of strings:
myApp.ComboPlaceHolderLabel,
myApp.GridTitleLabel);
myApp.SummaryLabel + '</b></div>');
myApp.NoneLabel + ')') + '</label></div>';
I would like to leave just myApp.[variable] and get rid of, e.g. ,, );, + '...', etc.
Using Notepad++, I can match the strings themselves using ^myApp.[a-zA-Z0-9].*?\b (it's a bit messy, but it works for what I need).
But in reality, I need negate that regex, to match everything at the end, so I can replace it with a blank.
You don't need to go for negation. Just put your regex within capturing groups and add an extra .*$ at the last. $ matches the end of a line. All the matched characters(whole line) are replaced by the characters which are present inside the first captured group. .
matches any character, so you need to escape the dot to match a literal dot.
^(myApp\.[a-zA-Z0-9].*?\b).*$
Replacement string:
\1
DEMO
OR
Match only the following characters and then replace it with an empty string.
\b[,); +]+.*$
DEMO
I think this works equally as well:
^(myApp.\w+).*$
Replacement string:
\1
From difference between \w and \b regular expression meta characters:
\w stands for "word character", usually [A-Za-z0-9_]. Notice the inclusion of the underscore and digits.
(^.*?\.[a-zA-Z]+)(.*)$
Use this.Replace by
$1
See demo.
http://regex101.com/r/lU7jH1/5