What regular expression can I use to find this?
&v=15151651616
Where &v= is a static string and the number part may vary.
"^&v=[0-9]+$" if you want at least 1 number or "^&v=[0-9]*$" if no number must match too.
If you want it to match inside another sequence just remove the ^ and $, which means the sequence beginning by (^) and sequence ending with ($)
You can use the following regular expression:
&v=\d+
This matches &v= and then one or more digits.
I tried the other solutions but those were not working for me but the following worked.
NAME(column):
dbbdb
abcdef=1244
abc =123sfdafs
abc= 1223 adsfa
abc = 1323def
abcasdafs =adfd 1323def
To find 'bc' followed by a number, Code:
. -> match any character
? -> optional (show even if there are no characters)
+ -> in addition to the search keyword
where regexp_like (NAME, 'bc.?+[0-9]');
Output:
abcdef=1244
abc =123sfdafs
abc= 1223 adsfa
abc = 1323def
abcasdafs =adfd 1323def
To find 'bc' followed by '=' and a number, no matter the spaces, Code:
where regexp_like (NAME, 'bc ?+[=] ?+[0-9]');
Output:
abc =123sfdafs
abc= 1223 adsfa
abc = 1323def
Related
My input is of this format: (xxx)yyyy(zz)(eee)fff where {x,y,z,e,f} are all numbers. But fff is optional though.
Input: x = (123)4567(89)(660)
Expected output: Only the eeepart i.e. the number inside 3rd "()" i.e. 660 in my example.
I am able to achieve this so far:
re.search("\((\d*)\)", x).group()
Output: (123)
Expected: (660)
I am surely missing something fundamental. Please advise.
Edit 1: Just added fff to the input data format.
You could find all those matches that have round braces (), and print the third match with findall
import re
n = "(123)4567(89)(660)999"
r = re.findall("\(\d*\)", n)
print(r[2])
Output:
(660)
The (eee) part is identical to the (xxx) part in your regex. If you don't provide an anchor, or some sequencing requirement, then an unanchored search will match the first thing it finds, which is (xxx) in your case.
If you know the (eee) always appears at the end of the string, you could append an "at-end" anchor ($) to force the match at the end. Or perhaps you could append a following character, like a space or comma or something.
Otherwise, you might do well to match the other parts of the pattern and not capture them:
pattern = r'[0-9()]{13}\((\d{3})\)'
If you want to get the third group of numbers in brackets, you need to skip the first two groups which you can do with a repeating non-capturing group which looks for a set of digits enclosed in () followed by some number of non ( characters:
x = '(123)4567(89)(660)'
print(re.search("(?:\(\d+\)[^(]*){2}(\(\d+\))", x).group(1))
Output:
(660)
Demo on rextester
I need to cut some numbers:
21250000.022000 -> 21250000.022
20.00 -> 20
200 -> 200
20.50 -> 20.5
UPDATE:
This is what i try:
https://regex101.com/r/Sdfe5D/1
In this regex the problem is with the numnbers '200' and '20'.
You don't need a regex for this. Just cast your string to double:
$arr = array('21250000.022000', '20.00', '200', '20.50');
foreach ($arr as $n)
echo (double) $n. "\n";
Output:
21250000.022
20
200
20.5
Code Demo
Update: If you're looking for a regex solution then use:
Search regex:
(?:(\.\d*?[1-9]+)|\.)0*$
replacement:
$1
RegEx Demo
You can use the following regular expression. I used a positive look-ahead to only select the relevant numbers: (?=)
^[^\.]+?(?=\.0*$)|^[^\.]+?\..*?(?=0*$)|^[^\.]*$
^[^\.]+?(?=\.0*$): The first part looks for a number which is not a dot ([^\.]), followed by a dot and a random number of zeros at, which continue until the end ($). This matches numbers like 20.000 -> 20 or 25.0 -> 25. It won't match any dots at the end of the line.
^[^\.]+?\..*?(?=0*$): The second part looks is pretty similar. It looks for a number which is not a dot, followed by a dot, any characters and trailing zeros. The difference is in the brackets of the positive look-ahead: This means, it matches also characters after the dot (\.), but only if they are non-trailing zeros. This regex alone would match 20.0 -> 20.. Examples for matches here are 20.04020000 -> 20.0402 or 24.120300 -> 24.1203
^[^\.]*$: matches full numbers without any dot in it. This one is necessary for numbers like 200 or 21.
This regular expression is based on the beginning (^) and the end of a string ($). It won't match multiple numbers in one string. When using multiple numbers in one string, line breaks need to be used to match the numbers.
You can test the regular expression with the following snippet:
// expected results:
// 21250000.022000 -> 21250000.022
// 20.00 -> 20
// 200 -> 200
// 20.50 -> 20.5
var regex = /^[^\.]+?(?=\.0*$)|^[^\.]+?\..*?(?=0*$)|^[^\.]*$/g;
var texts = [
'21250000.022000',
'20.00',
'200',
'20.50'
];
for(var i = 0; i < texts.length; i++) {
var text = texts[i];
console.log(text, '->', text.match(regex)[0]);
}
This regex has better performance:
\.0+$|(\.\d*[1-9])(0+)$
Replace with $1
Here's an explanation, if you're interested:
First, we deal with any whole numbers, since they're easier:
\.0+$
Then, we match the important numbers (That we want to keep) after the decimal point in a capturing group:
(\.\d*[1-9])
Finally, we match all trailling zeroes, and the end of the string (replace $ with \b if needed)
(0+)$
See it live
If you really need to use regex for this, following would be one way:
preg_replace("/(\.[0-9]*[1-9]+)[0]+/", "$1", "21250000.022000");
However, the trailing zeroes can be removed simply by casting the string to a double:
(double) "21250000.022000";
I'm trying to extract a number from a log file that outputs lines of text like this:
1/11/2016 3:26:12 AM 1/11/2016 3:27:00 AM 45.6 A
The output from the line is 45.6 A
However, my Regex code is returning the 12 A from 3:26:12 AM. I need it to completely ignore the time number and just output the 45.6 A.
Here's my Regex code:
$regex = '\d+(?:\.\d+)?(?=\s+A)'
You just forgot to anchor the lookeahead at the end of the string:
\d+(?:\.\d+)?(?=\s+A$)
^
See the regex demo
The \d+(?:\.\d+)? will match one or more digits optionally followed with a . followed with one or more digits (a float value), and the (?=\s+A$) lookahead will require one or more whitespace characters with A right at the end of the string to appear after the float value.
$s = '1/11/2016 3:26:12 AM 1/11/2016 3:27:00 AM 45.6 A'
$rx = '\d+(?:\.\d+)?(?=\s+A$)'
$result = [regex]::Match($s, $rx, 'RightToLeft')
if ($result) { $result.Value; }
You can use word boundary (\b) to match only A, not AM:
\d+(?:\.\d+)?(?=\s+A\b)
DEMO: https://regex101.com/r/pA7jK2/1
if you just need find the last digit with an A in it, try this
(\d+\.\d\sA)
Demo here
I've been trying to solve this problem in the last few days with no success. I have the following string:
comment = '#disabled, Fc = 200Hz'
What I need to do is: if there's the string 'disabled' it needs to be matched. Otherwise I need to match the number that comes before 'Hz'.
The closest solution I found so far was:
regexpi(comment,'\<#disabled\>|\w*Hz\>','match') ;
It will match the word '#disabled' or anything that comes before 'Hz'. Problem is that when it first finds '#disabled#' it copies also the result '200Hz'.
So I'm getting:
ans = '#disabled' '200Hz'
Summing up, I need to select only the 'disabled' part of a string if there is one, otherwise I need to get the number before 'Hz'.
Can someone give me a hand ?
Suppose your input is:
comment = {'#disabled, Fc = 200Hz';
'Fc = 300Hz'}
The regular expression (match disabled if follows # otherwise match digits if they are followed by Hz):
regexp(comment, '(?<=^#)disabled|\d+(?=Hz)','match','once')
Explaining it:
^# - match # at the beginning of the line
(?<=expr)disabled - match disabled if follows expr
expr1 | expr2 - otherwise match expr2
\d+ - match 1 or more digits, equivalently [0-9]+
expr(?=Hz) - match expr only if followed by 'Hz'
Diagram:
Debuggex Demo
[15-]
[41-(32)]
[48-(45)]
[70-15]
[40-(64)]
[(128)-42]
[(128)-56]
I have these values for which I want to extract the value not in curled brackets. If there is more than one, then add them together.
What is the regular expression to do this?
So the solution would look like this:
[15-] -> 15
[41-(32)] -> 41
[48-(45)] -> 48
[70-15] -> 85
[40-(64)] -> 40
[(128)-42] -> 42
[(128)-56] -> 56
You would be over complicating if you go for a regex approach (in this case, at least), also, regular expressions does not support mathematical operations, as pointed out by #richardtallent.
You can use an approach as shown here to extract a substring which omits the initial and final square brackets, and then, use the Split (as shown here) and split the string in two using the dash sign. Lastly, use the Instr function (as shown here) to see if any of the substrings that the split yielded contains a bracket.
If any of the substrings contain a bracket, then, they are omitted from the addition, or they are added up if otherwise.
Regular expressions does not support performing math on the terms. You can loop through the groups that are matched and perform the math outside of Regex.
Here's the pattern to extract any number within the square brackets that are not in cury brackets:
\[
(?:(?:\d+|\([^\)]*\))-)*
(\d+)
(?:-[^\]]*)*
\]
Each number will be returned in $1.
This works by looking for a number that is prefixed by any number of "words" separated by dashes, where the "words" are either numbers themselves or parenthesized strings, and followed by, optionally, a dash and some other stuff before hitting the end brace.
If VBA's RegEx doesn't support uncaptured groups (?:), remove all of the ?:'s and your captured numbers will be in $3 instead.
A simpler pattern also works:
\[
(?:[^\]]*-)*
(\d+)
(?:-[^\]]*)*
\]
This simply looks for numbers delimited by dashes and allowing for the number to be at the beginning or end.
Private Sub regEx()
Dim RegexObj As New VBScript_RegExp_55.RegExp
RegexObj.Pattern = "\[(\(?[0-9]*?\)?)-(\(?[0-9]*?\)?)\]"
Dim str As String
str = "[15-]"
Dim Match As Object
Set Match = RegexObj.Execute(str)
Dim result As Integer
Dim value1 As Integer
Dim value2 As Integer
If Not InStr(1, Match.Item(0).submatches.Item(0), "(", 1) Then
value1 = Match.Item(0).submatches.Item(0)
End If
If Not InStr(1, Match.Item(0).submatches.Item(1), "(", 1) And Not Match.Item(0).submatches.Item(1) = "" Then
value2 = Match.Item(0).submatches.Item(1)
End If
result = value1 + value2
MsgBox (result)
End Sub
Fill [15-] with the other strings.
Ok! It's been 6 years and 6 months since the question was posted. Still, for anyone looking for something like that maybe now or in the future...
Step 1:
Trim Leading and Trailing Spaces, if any
Step 2:
Find/Search:
\]|\[|\(.*\)
Replace With:
<Leave this field Empty>
Step 3:
Trim Leading and Trailing Spaces, if any
Step 4:
Find/Search:
^-|-$
Replace With:
<Leave this field Empty>
Step 5:
Find/Search:
-
Replace With:
\+