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

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.

Related

Finding first five digits in a var with 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 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.

want help on regular expression [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
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

Regex for two digit number followed by . for find and replace in vim [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 want to find out following string pattern in vim and replace it with some thing else. Can you please tell me regex for the same.
1.
11.
20.
21.
99.
basically one / two digits followed by dot.
I think you could do something like the following (I'm not very experienced with VI so there might be a better way)
:%s/\d\+\./MyString/gc
So that's essentially using \d\+\. to search for numbers appearing one or more times followed by a ..
MyString is your replacement string.
:%s is the substitute command, :s would just search the current line.
/gc looks for the match as many times as it appears on the line (g), and asks for confirmation before each replacement (c).
Tried this?
[^0-9][0-9][0-9]\.
Or have you tried it and it didn't work?
It has an issue though of three digits and a dot, i.e. "123." will also be captured
The regex for 1 or 2 digits followed by a dot is:
\<\d\d\?\.
The "word boundary" \< precludes 3 digits (and a dot), which would be allowed without it (the last two digits of a 3-digit number would match).
To replace using this tegex in vi:
s/\<\d\d\?\./foo/g

Can Regular Expressions search for groups no matter the order or whether they all exist? [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 8 years ago.
Improve this question
So I want to search for A,B,C,D in a string in any order, but if C doesn't exist I still want it to give me A,B, and D, etc.
To be more specific, here is the exact problem I'm trying to solve. CSV file with lines that look like this:
Name,(W)5555555,(H)5555555,(M)5555555,(P)5555555
However, the W,H,M,P could be in any order. Plus they don't all exist on every line. So it looks more like this:
Name,(W)5555555,(H)5555555,(M)5555555,(P)5555555
Name,(H)5555555,(P)5555555,(W)5555555,(M)5555555
Name,(M)5555555,(H)5555555,,
Name,(P)5555555,,,
What I need to accomplish is to put all items in the correct order so they line up under the correct columns. So the above should look like this when I'm done:
Name,(W)5555555,(H)5555555,(M)5555555,(P)5555555
Name,(W)5555555,(H)5555555,(M)5555555,(P)5555555
Name,,(H)5555555,(M)5555555,
Name,,,,(P)5555555
Edit: It appears I'm a bad Stack Overflow citizen. I didn't get answers fast enough for when my project needed to be done, and therefore forgot to come back and add a correct issues in my post. I ended up writing a python script to do this instead of just using find/replace in BBEdit or Sublime Text 2 like I was originally trying to do.
So I would like a method to do something like this that works in either BBEdit or Sublime Text. Or Vim for that matter. I'll try to keep a better eye on it this time, and I'll respond to the answers that already exist.
If your regex flavor supports lookarounds, this can be done with a simple regex-replace. Since lookaheads do not advance the position of the regex engine's cursor, we can use them to look for multiple patterns somewhere after one particular position. We can capture all these findings and write them back in the replacement string. To make sure that all of them are optional we could simply use ?, but in this case, I'll add an empty alternative to the lookahead - this is necessary to trick the engine when it's backtracking. The pattern could then look like this:
^Name,(?=.*([(]W[)]\d+)|)(?=.*([(]H[)]\d+)|)(?=.*([(]M[)]\d+)|)(?=.*([(]P[)]\d+)|).*
The .* at the end is to make sure that everything gets removed in the replacement.
And the replacement string like this:
Name,$1,$2,$3,$4
Here is a working demo using the ECMAScript flavor. It's a rather limited flavor, so this solution should be adaptable to most environments.
Something like this?
^Name,(\((?:W|H|P|M)\)\d+(?:,)?)*[,]*$
Edit live on Debuggex
Will give you all the matches per row. Then you simple need to allocate each match to the right column.

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.