Regex everything before (-) if matches 7 characters [duplicate] - regex

This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 2 years ago.
Good morning,
Could tou please help on this regular expression request.
I would like all characters before "-", but only if that chain has a lenght of 7 characters
If 5QHTN33-48314742, result 5QHTN33. But if AAA5QHTN33-48314742, then no result.
Thanks for your help.

as '^' refers to start of line and '$' refers to end of line, I highly prefer to say you have to split the line with spaces(regex of split: (\s+) , for java: (\\s+)) so you can use below regex after splitting and use it on every element, hope to be helpful:
(^)((?<word>[\w\W]{7})\-.+)($)
Explanation
(?<word>[\w\W]{7}) will capture your word into a group with name word ,so you can get it easier.

Related

RegEx - get only last asterisk [duplicate]

This question already has an answer here:
Regex match strings between asterisks
(1 answer)
Closed 2 years ago.
Is it possible to do a RegEx to only match the last asterisk right before and first asterisk right after an value?
Value: **example**
My actual regex: /\*(.*?)\*/g
My actual regex matches the two asterisks before and after word example, but i want to do a regex that only matches the last asterisk right before and the first asterisk right after the word.
Thank you!
try /[*][^*]+[*]/gm
Works as tested on regex101.com

Match character and any whitespaces around it [duplicate]

This question already has answers here:
Splitting a String by number of delimiters
(2 answers)
Closed 2 years ago.
I have a file containing informations in the following format :
Fred,Frank , Marcel Godwin , Marion,Ryan
I need the match commas and any whitespace around them, but not any comma inside brackets.
My problem is that with my current regex [\s,]+ the whitespaces between words are matched. So in this example the whitespace between Marcel and Godwin.
I thought about using something like \s,\s* but it wouldn't match parts when there is no whitespace around the comma, like between Fred and Frank
Surely, it's a simple fix but I can't figure it out.
I think this will match the commas including the whitespace before and afterwards like you explained in your question.
\s*(?=\,)\,(?<=\,)\s*
This is a positive looahead: (?=\,), it means it matches any whitespace if there is a comma afterwards.
This is a positive lookbehind: (?<=\,), it means it matches any whitespace if there is a comma rigth before.
Try it out yourself. You can use this page to check the output in your browser.

match any character behind / (front shash) [duplicate]

This question already has answers here:
Regex: matching up to the first occurrence of a character
(14 answers)
Closed 4 years ago.
can someone please quickly help me with regularexpression to match any character and group it which is coming behind front shash:
ae-app001/UK/Server/company
in this i want to match ae-app001 only.
the format remains the same except that we dont know how many slash might be there,
sometimes the string might be just :
ae-app001/UK/Server
so i need a generic regex which will match string1 in the below:
string1/string2/string3/string4
string1/string2/string3
etc..
/^([^\/]+)/ Demo
But what if there's no slashes? Is it ok to capture whole string?

python regex for finding ssn in text not working [duplicate]

This question already has an answer here:
Parse IP addresses from txt
(1 answer)
Closed 7 years ago.
>>> pat=re.compile('^\d{3}-\d{2}-\d{4}$')
>>> pat.findall('my sssn is 111-22-3333')
I am trying to catch ssn in the text. I tried the expression in pythex and it worked there but it's not wokring in python. I am new to this.
Remove the ^ and the $ anchors:
Your regex should be:
\d{3}-\d{2}-\d{4}
The caret ^ matches the position before the first character in the string, and since you have m in your input, \d{3} doesn't match.
$ matches after the last character in the string, you don't really need it here unless you want nothing to appear after the last four digits.
pat=re.compile('^.*?(\d{3}-\d{2}-\d{4}).*$')
Just group what you want and use .* to catch the buffer.This will make ^$match the whole string as opposed to what you were doing as then there were character after and before what you wanted.

How is this regex [...]+ processessed? [duplicate]

This question already has an answer here:
Learning Regular Expressions [closed]
(1 answer)
Closed 8 years ago.
I am new to perl (beginner, learning perl for past 1 week during spare time). This is my first programming language.
I want to know how this regex []+ works in perl. I have 3 questions.
What will this do: if /[\d\s\.,:\/]+/?
I learned if /.../ matches pattern.
So will it match the following?
And which parts of the following will not match?
335.31, 312.52
Dave1.532
Path: "./1243/453 /48.1"
543, 546
Edit:
This is not a duplicate of the linked question as I am specifically asking how []+ works. The answer in the linked post does not cover this.
I know what each character in the regex I have written above represents and how each character work. What I want to know is how []+ will influence the regular expression. Specifically how the + will influence the [].
I suggest you use regex101.com to try the regular expression. Below is the breakdown for the expression you provided:
`[]` match a single character present in the list
`+` matches one or more of the above
`\d` match a digit [0-9]
`\s` match any white space character `[\r\n\t\f ]`
`\.` matches the character . literally
`,:` a single character in the list ,: literally
`\/` matches the character / literally
You'll get the following matches (if you run this with g- global option) - (REGEX sample - ref):
`335.31, 312.52 `
`1.532 `
`: `
`./1243/453 /48.1`
` 543, 546 `