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

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?

Related

How to get the reversed result of the following regex? [duplicate]

This question already has answers here:
How can I "inverse match" with regex?
(10 answers)
Closed 6 months ago.
Regex: /^[0-9\p{L}.,\s]+$/u
I would like to replace the characters not matching with the regex with "".
As I understand, you simply want to drop all chars not matching your regex. So the idea is to invert the class of chars:
/^[0-9\p{L}.,\s]+$/u should become /[^\d\p{L}.,\s]+/gu (I added the ^ after the [ to say "not in this list of chars" and replaced 0-9 by \d for digits. Use the g modifier (=global ) to match multiple times.
Running it: https://regex101.com/r/IQz6K5/1
I'm not sure that ,, . and the space will be enough ponctuation. It would be interesting to have a complete example of what you are trying to achieve. You could use another unicode character class for ponctuation if needed, typically with \p{P}. See more info about unicode classes here: https://www.regular-expressions.info/unicode.html#category

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

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.

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.

Adding words to a character class in Regex [duplicate]

This question already has answers here:
Regular expression to match a line that doesn't contain a word
(34 answers)
Closed 2 years ago.
I am currently using the following character class:
[^\)\(] in my regex
I want to add the word 'hello' to this class so it is also not matched in my string.
I have tried
[^\)\((hello)]
but it does not work.
What can I do?
One typical way you would enforce that hello does not appear would be to use a negative lookahead, e.g.
^(?!.*hello)[^t()]+$
If you only wanted to exclude hello when it appears as a bona fide word, then surround it with word boundaries in the lookahead:
^(?!.*\bhello\b)[^t()]+$

Selecting words in a text using regex [duplicate]

This question already has answers here:
Regex match entire words only
(7 answers)
Closed 7 years ago.
var text = 'word otherword';
I want to select words that only matches with 'word'. When i use simply /word/ for regex pattern, then it also selects the 'word' part in 'otherword'. I dont need that word(in this case 'otherword').So how can i only select/match 'word' word using regex?
Use word boundary - \b.
/\bword\b/