|
Hello -
I have the following string:
[ CmdAV=show CmdArgAV=policy-map CmdArgAV=system CmdArgAV=type CmdArgAV=network-qos CmdArgAV=<cr> ]
Is it possible to use regex to leave only:
show policy-map system type network-qos <cr>
I don't need the [,], or CmdAV or CmdArgAV.
|
|
|
|
|
This should do the trick:
/C\w+=|[[\]]/g
|
|
|
|
|
Great, this does work. Thank you!
Is it possible to capture the remained words into a single group?
|
|
|
|
|
Hello,
I have a question on regex: Is it possible to match for certain characters (and replace them) in only a part of a string? E. g.
str="I want to solve this problem"
I now want to match all spaces after the first occurence of the letter, say, "v". I know I can filter the whole part of the string after (and including) the first v with the regex "v.*", but how to match the spaces only in that part?
Thank you!
|
|
|
|
|
Depends on the regex engine you're using.
For example, in C#, you can use a zero-width positive look-behind assertion[^]:
Regex re = new Regex("(?<=.*v.*)\s+");
string[] parts = re.Split("I want to solve this problem");
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Thanks, Richard, for your answer!
I am using regex with javascript, developing for popular browsers.
So, if I understand correctly, your code splits the string into 3 parts, the first part being everything until the first space after the first "v", the other parts being the rest of the string split by spaces. I am sure that will work.
Well, what I was looking for - this is a more theoretical and general question - is there an operator you can use in regexes which does something like "apply the following only to the previously matched part"? For example:
/v.*#c/g
Explanation: The first part of the regex is "v.*" which means: Match the part of the string after the first v. The second part is "c", which will match every c. Now, is there an operator (here symolized by #) between the 2 parts of the regex, which means: "Apply the part after this operator ("c") to the result of the part before the operator ("v.*)"? It is like: Do a secondary match inside the primary match.
Sorry, English is not my native language, but I still hope, I could make my point clear.
Thank you.
|
|
|
|
|
Look-behind is the way to go:
(?<=Y)X matches X , but only if there's Y before it
Demo[^]
It's supported in the Javascript regex engine for most modern browsers:
Can I use... Lookbehind in JS regular expressions[^]
The only hold-outs are Internet Explorer - which even Microsoft agree should not be used any more - and Safari.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Great, that was exactly what i was looking for, thank you so much, Richard!
So, to solve the problem from my original posting, that is to match all spaces after the first "v" in the string "I want to solve this problem", this Regex works:
/(?<=v.*) /g
|
|
|
|
|
Hi Would like to know if it's possible to get only the first match for this regex
^ \(SQL\)Max Duration..* ([0-9]+[0-9])( \([0-9][0-9][0-9][0-9])
I was able to isolate the value in second group but I am only interested to get the first match.
(SQL)Max Duration.. 1132 (2021-02-21 07:17:46:095)
(SQL)Std. Dev...... 3.36
(SQL)Max Duration.. 13 (2021-02-21 07:05:43:582)
|
|
|
|
|
Can you explain more what you're trying to do? Also, what tools/language are you using?
|
|
|
|
|
Hi there:
Suppose I have a string with "1: a string, 2: other 5 string, 3: something 8; else", what would be the regex pattern to obtain "1,2,3" as a result
Thank you in advance,
Rafael
|
|
|
|
|
You don't say what environment you are working in, so it's hard to give you useful code.
However, you can probably get what you want by either doing a search & replace operation, possibly repeated, or by using some kind of "regex iterator" approach.
For C++, it would be a regex iterator (iterate over all the matches of THIS pattern in THAT string). For something like Python it would be find all occurrences of THIS pattern in THAT string.
In order to drop the "8;" from your example sentence, you'd want to provide a better pattern than just "some digits." Instead, use something like /(\d+):/ to match on one-or-more digits followed by a colon. You want to capture the digits, not the colon.
If you can just iterate over all matches in the string, that will probably be enough. If you're doing sed or some other editor, you'll have to search and replace the line. If possible, see if you can do something like a non-greedy match (.*?) to match all the text between matches, and then replace it with a comma. (FYI: The ? operator is a non-greedy modifier in Perl-style regex engines. Other regex engines may not support this at all, or they may have a different syntax-- for example, Vim would use .\{-} for that.
|
|
|
|
|
Hello Community,
I have created Regex with Regex Magic with HTML 5 Firework, see attached.
The Regex [A-Za-z]{3}\d{4} successfully finds a string such as GMK6954 using Character Spaces.
Can someone help with modifying the Regex such that it replaces the first 3 letters with --- so the resulting Regex will generate the following
6954
Thanks
|
|
|
|
|
Change you digits to a "excluded suffix" and use String.prototype.replace() - JavaScript | MDN[^]:
[A-Za-z]{3}(?=\d{4})
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Hi Griff,
Thanks for reaching out. Unfortunately, it didn't work.
The replacement just returns
[A-Za-z]{3}(?=\d{4})
|
|
|
|
|
And how - exactly - did you try it?
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Is it possible to send an attachment on this forum
I would like to send a screenshot to you.
|
|
|
|
|
No. Copy'n'paste the relevant code fragments.
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Hi, sorry for the delayed response.
I wanted to show you a screenshot, because I'm not getting an error from the application I'm using. It's replacing the string with [A-Za-z]{3}(?=\d{4})
Let me know if you can see the screenshot from the link below?
regmagic2.png - Google Drive[^]
|
|
|
|
|
And what do you think that tells me about how you coded it?
To quote myself:
Quote: And how - exactly - did you try it?
If you go for a drive and break down, when you call the garage, do you send them a picture of the view you can see out the car window? Or tell them what happened and where you are?
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
I dont understand.
Did you see the screenshot?
I simply cut-and-paste your regex
I should point out the regex I originally sent was not generated by me - I simply got the regex using an application called RegexMagic.
I am 100% new to Regex.
This means, I really don't have a clue what you're referring to.
Please help
|
|
|
|
|
Code.
A regex doesn't exist in isolation - it has to be used in code in order to do anything. On it's own, it's just a string ...
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
What does it take to get you to copy and paste the code you're using where you're setting up and using this RegEx?
|
|
|
|
|
Hi Dave,
Sorry for the delayed response
The reason for wanting to show a screenshot is because I use application to insert the a Search and Replace regex. So I would enter [A-Za-z]{3}\d{4} in the Search dialogue box, and [A-Za-z]{3}(?=\d{4}) in the replace dialoge box.
The search regex works fine, but the replace regex returns [A-Za-z]{3}(?=\d{4})
I'm assuming the the Regex pattern HTML 5 FireFox doesn't allow for replace?
|
|
|
|
|
You're making a lot of baseless assumptions and STILL not providing what is needed to solve the problem.
|
|
|
|