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.
Related
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 have a large chunk of data and would like to extract a value of a particular field using TCL regexp.
ip="1.2.3.4" protocol="SFTP" username="abcd"
Need to extract the word SFTP without double quotes, the former and later fields can be ip,username or something else. So regexp has to use the word protocol as reference.
In this case, I'd use:
regexp {\yprotocol="(.*?)"} $theString -> theProtocol
However, if this is parsing XML then I'd actually use an XML handling extension like tDOM.
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 have a variable that looks like this: AF1400006
I want to use regular expressions in order to return the number "14000".
I have gone through many threads here, but none quite seems to get me anywhere.
Thanks!
EDIT:
I don't see what's up with the downvotes, isn't this a forum for asking questions?
Anyhow, I solved my problem now, thanks for the help :-) For those wondering, I used it in a scanning software called Drivve Image to use parts of a barcode on a document as the name of the output folder. The software uses a unique regex formatting which seemed to be my issue.
You would need to use something like \d{5} which will match 5 digits.
Depending on the language you are using you would then access whatever the group matched.
For instance, in Java:
String str ="AF1400006";
Pattern p = Pattern.compile("\\d{5}");
Matcher m = p.matcher(str);
if(m.find())
System.out.println(m.group());
Which yields the number you are after.
An example of the expression is available here.
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 am trying to make regex for this type of string (hello(world))
i made this regular expression \([a-zA-Z]\([a-zA-Z])*\)\) but it gives error.... what should i have to do to make it work properly....
In regular expressions, brackets are a special character. If you want to match them literally, you need to escape them with a backslash: \(. Edit: looks like you escaped most of them, but it wasn't code formatted in your question. Fixed that, and there was only one missing escaped ).
You also are missing a quantifier (in this case another * or a + would work) that would enable it match your string, and the one you've got there is in the wrong position. This should work for you:
\([a-zA-Z]*\([a-zA-Z]*\)\)
Here's a regex101 to play around with: https://regex101.com/r/kQ0xT0/1
You can also use the /i case-insensitive modifier with most regex languages - this lets you just write [a-z] instead of [a-zA-Z]. See https://regex101.com/r/pT9kV1/1
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
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.