want help on regular expression [closed] - regex

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I have a string like '/whatever/whatver/whatever/v{int}'
As the string shows, the string is compose of some prefix '/whaterver',the string has prefix or not,either is ok and the last part is /v{int}
I wrote a regular expression (^[/][a-zA-Z]+)*/v[0-9]+, (^[/][a-zA-Z]+)* means the prefix can appear or not and /v[0-9]+ to match the last part.
I guess the expression can fully match the string, but it doesn't.
Can any one help me ?

First of all, why the negation of / character?
Second, I can see you can have multiple prefixes, so you have to check also the multiple cardinality of prefixes (see the * wildcard I added).
This expression ([/][a-zA-Z]+)*/v[0-9]+ matches your input and also matches input with no prefix.
EDIT
In case you want to keep the beginning-of-line marker (even though it's redundant if you want an exact match), you need to exclude it form the repetitive block (marked with * wildcard), because you don't want the engine to look for new line beginnings in each prefix.
^([/][a-zA-Z]+)*/v[0-9]+

echo "/whatever/whatver/whatever/v1654" | grep -E '^/?([a-zA-Z]+/)+v[0-9]+$'
/whatever/whatver/whatever/v1654

Related

How to remove replace all characters except specified ones using regex? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I would like to remove parentheses from a string using a Regex. I see a lot of examples removing numbers or letters but those are not my case.
Thank you all
Try:
s/[()]//g
This is additional text to pass length requirements.
Try
sed 's/[()]//g' file
The regular expression [()] matches a single character which can be a left or right round parenthesis. The sed substitution operator s accepts a flag /g which says to repeat the substitution for every occurrence on each input line.

Regex match every occurance and wildcard condition [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
How can I write a regex that will match the following conditions:
Where every instance of the occurance 'eVar7=' contains the following 'unav'
This should be false:
;AA;;;;eVar7=nr_unav,;AA;;;;eVar7=br_unavail,;AA;;;;eVar7=sugg,;AA;;;;eVar7=sugg,;AA;;;;eVar7=sugg
and this should return true:
;AA;;;;eVar7=nr_unav,;AA;;;;eVar7=br_unavail,;AA;;;;eVar7=nr_unav,;AA;;;;eVar7=nr_unav11,;AA;;;;eVar7=nr_unavasdfasdferwgf
This regex does the opposite. If there is a match means that you don't want it:
eVar7=(?![^;]*?unav)
It uses a negative lookahead. Meaning that after the eVar7 match there should not be something with "unav" in the middle. If so, it matches.
This is just to put you on the right track however as there are a lot to consider with your examples. For example where exactly should this "unav" pattern exist. What characters are allowed between eVar7 and "unav" etc. Feel free to change the regex to suit your needs.

How to start a RegEx statement at the 2nd position of a string [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a string
.HACK G.U. VOL. 1 REBIRTH
I'm using a tool that allows me to specify a RegEx statement that can be used in a replace operation.
I want the RegEx to find all the periods "." that start after the 1st position. The replace operation should return the following.
.HACK GU VOL 1 REBIRTH
Thanks
The following will do the trick:
(?<!^)(\.)
per http://rubular.com/r/w1apzTZLPk
Since Javascript doesn't support negative lookbehind, this can't be done in Javascript, but there are alternatives as discussed in http://blog.stevenlevithan.com/archives/mimic-lookbehind-javascript
One approach in this case would be to capture the previous character and replace it with the same content as part of the replacement process, as in:
(.)(\.)
Note: You don't need to use a capture group for the matching of the literal . in either of the above. I just used that technique to highlight the match in Rubular.

Regex replace hexadecimal characters [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
I am new to regex and unable to create a expression to resolve my problem.
I have a string like /abc/def%20hi%28hello%29test. I would like to replace the hexadecimal characters, that is, %20, %28 and %29 with -
I know all three of them have different meanings, but I am fine with replacing all with any single character, I mean - or even . or ' '(space).
So, I would like to get the output as /abc/def-hi-hello-test.
I thought to find % and replace three characters from %[a-zA-Z0-9][a-zA-Z0-9] with -, but doesn't work.
Can some please help me write a regex which just replaces those hexadecimal characters?
Search for:
%[a-fA-F0-9][a-fA-F0-9]
replace with:
-
Example: http://regex101.com/r/qR6xW7
It sounds like what you really want to do is decode the URL encoding, and I will bet you that your host language has a facility for that built in. In PHP, that's url_decode.
After you've decoded the URL, then you can modify what it is you want to modify more easily.

Regex to get string till it hits a comma [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
Let's say I have a string like this: "1367,14,243,540"(will always have 4 number and only numbers, no decimal places and always separated by comma)
How should the regex look like that would allow me to pick/filter out/return lets say 243 from the string?
here is your regex if you insist on a regex /\d+/g (g here is for multiple selections in js, use matches with Microsoft framework) though you can use split (example using js):
var v='123,333,445,67';
console.log('split:');
console.log(v.split(',').map(function(n){return parseInt(n);}));
console.log('\nregex:');
console.log(v.match(/\d+/g).map(function(n){return parseInt(n);}));
jsfiddle
the numbers will be returned in an array, you can use the index to access the desired one, let's say 2.
note: split is faster than regex, you can test the difference in performance using jsperf.com
Edit: For those who are interested in the performance difference, check this link.
note2: map here is just for parsing the strings into integers, you can remove it if you want to keep them as strings.
try
^([[:digit:]]+,){2}([[:digit:]]+)
your desired number is in capture group #2.
As one of the comments says, you shouldn't really use a regex in this case. Always try to use the appropriate tool for the job, and in this case the regex is HUGE overkill.
Your problem is solved easily as this
$sourceString = "1367,14,243,540";
$numbers = explode(",", $sourceString);
$neededNumber = $numbers[2];
You just need to describe your string:
^(\d+,){2}(\d+)
"From the start, number followed by comma appears two times, then another number."
You can pick the number of the second group, i.e. \2 or $2.