Python regular expression ".+" vs ".*" [duplicate] - regex

This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 5 years ago.
In Python 2.7, when i try re.findall("abc.","abcd abc"), I receive the expected answer "abcd" only
if I use re.findall("abc.asterisk*","abcd abc") I receive "abcd" "abc" as expected
but when I use re.findall('abc.+',"abcd abc") I receive "abcd" "abc" again instead of just "abcd"
Why?

As the comments says, * is for 0 or more matches, + is for 1 or more matches.
If you want exactly 1 match, you shuld do directly like:
abc.

Related

How to get specific data from String using regex in java? [duplicate]

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

Regex clarification (both return true?) powershell [duplicate]

This question already has answers here:
How do I match an entire string with a regex?
(8 answers)
Regex.Match whole words
(4 answers)
Closed 5 years ago.
I'm trying to match a pattern in powershell, any 3 letters followed by any 2 numbers, example: abc00
Trying:
'abc00' -match '[a-zA-Z]{3}[0-9]{2}'
returns TRUE
but :
'abcdefg0000' -match '[a-zA-Z]{3}[_0-9]{2}'
also returns TRUE.
How do I limit it so if the string doesn't contain exactly 3 letters and 3 numbers, it returns false?
Thank you!

Golang split strings by char type [duplicate]

This question already has answers here:
Regex split numbers and letter groups without spaces
(2 answers)
Closed 5 years ago.
I want to be able to split the string into substrings where characters and numbers are separate groups:
re:=regexp.MustCompile("**MAGIC HERE**")
fmt.Println(re.FindAllString("abc123def", -1))
I want to be able to get
[abc 123 def]
Any ideas?
Try splitting on this pattern:
\d+|\D+
Code:
re:=regexp.MustCompile("\\d+|\\D+")
fmt.Println(re.FindAllString("abc123def", -1))
Output:
[abc 123 def]
Demo here:
Rextester

Regex for any name [duplicate]

This question already has an answer here:
Learning Regular Expressions [closed]
(1 answer)
Closed 5 years ago.
I want regex with at least 2 characters start with any alphabet or any digit not matters.But It can accept - and _ .
Ex : ABD , Abc_123 , 12, A-_ , A1 etc.
(Updated)
(?=[-\w]{2}).*
Online test, https://regex101.com/r/JcUaBz/2
This should do the trick:
[\w-]{2,}
But, if you want to ignore words that have special characters, you can use this:
(?<=\s|^)([\w-]{2,})(?=\s+|$)

Regular expression for parsing a phone number [duplicate]

This question already has answers here:
How to validate phone numbers using regex
(43 answers)
Closed 7 years ago.
I need a regular expression for a phone number.
Example,
1234567899 should be (123)(456)(7899)
12345678 should be (123)(456)(78)
1234567 should be (123)(456)(7)
123456 should be (123)(456)
12345 should be (123)(45)
123 should be (123)
1 should be (1)
I tried /([0-9]{0,3})([0-9]{0,3})([0-9]{0,4})/ and /([0-9]{3})([0-9]{3})([0-9]{4})/
But it takes only when all the 10 numbers are in the input.
I need match then replace with (
This would appear to work:
/(\d{1,3})(\d{1,3})?(\d{1,4})?/
Live test here
Check this : /^\(\d{0,3}\)(\(\d{0,3}\))?(\(\d{0,4}\))?$/
https://regex101.com/r/qC0fS9/2
use this pattern
((^\d{1,3})|(?<=^\d{3})(\d{1,3})|(\d+$))
or simplified to
(^\d{3}|(?<=^\d{3})\d{3}|\d+$)
and replace with (\1)
Demo