This question already has answers here:
How to validate phone numbers using regex
(43 answers)
Closed 6 years ago.
I have a file that contains any of the following number format
12.456.7890
12-456-7890
123.456.7890
(123)456.7890
(123).456.7890
123-456-7890
(123)-456-7890
(123)456-7890
Is it possible to use regex substitution so that the final output number will always be on a format (123)456-7890 or (12)456-7890
Yes, it is:
s/\(?(\d\d\d)\)?[-.]?(\d\d\d)[-.]?(\d\d\d\d)/($1)$2-$3/g
I should mention that the above will also parse the following two:
123)456.7890
(123456.7890
You can do this using two substitutions:
perl -lpe 's/\D//g; s/(\d{3})(\d{3})(\d{4})/($1)$2-$3/' file
The first one removes all characters that aren't numeric. The second one inserts the desired characters between each group.
You should take into account that this approach will make a mess of any lines that aren't like the ones in your sample input. One means of protecting yourself could be something like this:
if ((#a = /\d/g ) == 10) { /* perform substitutions */ }
i.e. ensure that the number of matches on the line is 10 before proceeding.
Related
This question already has answers here:
Comma Separated Numbers Regex
(6 answers)
Closed 3 years ago.
I have the requirement to restrict a non-required textbox to only numbers (0-9) with a separator of ';'. The pattern is that the groups can be 4 or 5 in length and can repeat n times. Anything less than 4 in a group is invalid. After 4 or 5 I need to check for the existence of a separator character ';'. This pattern can repeat n times. I have tried variations of but this doesn't seem to be working. Something simple to start out like
[0-9]{4,5};+
is invalid as I don't need the separator for only 1 number grouping.
Next I tried
^[0-9]{4,5}|[0-9]{4,4};|[0-9]{5,5};$
but this doesn't work because the existence of four digits like 1111 or five digits 11111 before gives one match before it goes awry example "11111;j" Is there a way in a regex to validate
1111
11111
1111;1111
11111;1111
11111;11111
but catch
111
111;
1111;1
11111;1
abc
in a repeating fashion?
This validate your examples.
^[0-9]{4,5}(;[0-9]{4,5})?$
Try it
It's not clare what you mean by "in a repeating fashion". If you want validate also this
1111;11111;11111;1111;11111
You can use this regex
^[0-9]{4,5}(;[0-9]{4,5})*$
Try it
This question already has answers here:
how can I exctract attribute value using JAVA regex
(2 answers)
Closed 4 years ago.
I have a string.
String str= " <decision CCDBNUM=\"1111111\" adddate=\"20180112\"><decision CCDBNUM=\"2222222\" adddate=\"20180114\"> ";
I want to write a regex to fetch a particular value from this string.
My Expected Output is: to fetch only the value of CCDBNUM, i.e,
1111111 2222222
Please help me with this issue.
This should work:
CCDBNUM="([^"]+)"
Just get group 1 of each match.
I assume that CCDBNUM wouldn't contain the characters CCDBNUM. If that's not the case, I suggest you use an XML parser. Regex is not enough for that.
Demo
This question already has answers here:
Regex validation for numbers with comma separator
(3 answers)
Closed 4 years ago.
I have a problem with regex and need your help. I want to check my string is correct or incorrect. First and last is a number, only number and comma after it. No space inside 2 numbers.
Ex:
1,2,3,49,5 this is correct
1,2,3,45, this is incorrect
,12,4,2,67 this is incorrect
1,2 3,4,5,6 this is incorrect
^(?:\d+,)*\d+$
(?:\d+,)* - gets matches like "0," "00," "000," ... or empty
\d+ - gets last number as "0" "00" "000"
Please check below regex to solve your problem.
Regex: ^[0-9]+([0-9,])+[0-9]+$
^[0-9]+ is for start with one or more number
[0-9]+$ is for end with one or more number
([0-9,])+ is for one or more number with comma
Please check the output in Regex101
Update:
Please check the updated regex: ^(\d+,)+\d+$
^(\d+,)+ is for one or more number with comma and this will handle first number with comma
\d+$ is for end with one or more number
Please check the updated output in Regex101
This question already has answers here:
Getting the text that follows after the regex match
(5 answers)
Closed 4 years ago.
I have a line containing
[India,sn_GB] Welcome : { Name:{Customer1},Place:{Mumbai},}
I want to print the entire line after sn_GB] in splunk, which is
Welcome : { Name:{Customer1},Place:{Mumbai},}
I used the below regular expression:
(?<=sn_).*?$
But it prints, along with GB] like GB] Welcome : { Name:{Customer1},Place:{Mumbai},}.
In the word sn_GB, sn_ is constant and the rest two letter will vary, like GB, LB, KB, TB as such.
Please help me in correcting the regular expression.
Thanks
This will give the correct result in case sn_GB is constant.
(?<=sn_GB).*?$
If GB is not constant you can go for:
(?<=sn_...).*?$
I understand your question now.
Country codes are always 2 letters.
i'd use
(?<=sn_..\]\ ).*$
but you could use
(?<=sn_[A-Z]{0,5}\]\ \s*).*?$
(?<=sn_....).*$
is the simplest, as it will just grab 4 characters after, if it's always 2 letters for country code, and then a closing bracket and a space
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Regular expression for finding currency values but not dates in text
Hi this is my text can u please provide reg ex for finding currency only as shown below:
97.38
86.16
3,259.81
28,781.07
problem here is it is getting value from date also because it is in the of the currency so it should restrict the value from date
Ex text:
13/07/2011 EA35906558 - 13.07.11 8054 97.38
14/07/2011 EA35906566-14.07.11 8054 86.16
14/08#011 VP40853570 - 14.08.11 8122 3,259.81
14108/2011 VP50433270-14.08.11 8122- 28,781.07
Are the numbers always at the end of the line? If so:
\s([0-9]+\,?[0-9]+\.[0-9]+)$
http://rubular.com/r/8z7r8epLk9
That is actually very easy:
resultString = Regex.Match(subjectString, #"([\d.,]+)\s*$").Groups[1].Value;
Provided you care only for the last number of the string. If you apply the above in each line you will get your currency. No need for special format or anything. Implementation is .NET but you can change it to anything you want.