This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 6 years ago.
I am new to regex. What does regex expression match pattern "\[.*\]" mean?
If I have a text like "Hello [Here]", then success is returned in the match. And match contain [Here].
I read that:
. indicates Any except \n (newline),
* indicates 0 or more times
I don't understand the "\". It believe it is just escape sequence for "\".
So, is the expression "\[.*\]" trying to match a pattern like \[Any text\]?
Yes, you are right. It will match any characters enclosed in []. The .* imply any or no characters enclosed in [].
Also you should try this link which is a very helpful regex tool. You can input the regex pattern and check for matches easily.
I have tried this on regexr, here is a screen shot:
Related
This question already has answers here:
Regex whitespace word boundary
(3 answers)
Closed 4 years ago.
Regex -> ^([\s]*(?i)(section)[\s]*(?=[xvi])M*D?C{0,4}L?x{0,4}v{0,4}i{0,4})[\s]?
Text -> Section VII, some text.....
This regex matches Section VII and Section VII(space).
But it should not match "Section VII," , as after the match there is one special character.
What you need is a zero-width negative lookahead assertion:
^([\s]*(?i)(section)[\s]*(?=[xvi])M*D?C{0,4}L?x{0,4}v{0,4}i{0,4})(?!\S)
The critical part is (?!\S), which means that no (!) non-whitespace character (\S) should be at the end. It's a zero-width expression, which means that the character itself (if any) won't be a part of your match.
This question already has answers here:
Regex how to match an optional character
(5 answers)
Closed 5 years ago.
I have a simple Regex that looks for the words "won't connect" in a string.
(.*) won't connect.*
But I want to have it so it can ignore the apostrophe in case the user types in "wont connect".
(.*)won'?t connect.*
should be sufficient, matches " ' " 0 or 1 times
check the demo here
Your pattern:
(.*) won't connect.*
The * quantifier is greedy by default. That means your regex will go wrong with:
foo won't connect won't connect
See demonstration.
If you add a question-mark behind the quantifier, it will be lazy, meaning it will match only as much for the pattern to succeed. See an explanation here.
Improved pattern:
(.*?) won'?t connect.*
This question already has answers here:
Match exact string
(3 answers)
Closed 6 years ago.
I'm working on a pdf scraper in C# and I got stuck on a regex problem. I want to match just the account number and my regex statement is matching both the incorrect line and the correct line. I think I have to match everything until a new line but I can't find a way to do it.
This is my regex: ([A-Z0-9\-]{5,30})-[0-9]{1,10}-[0-9]{3}
XXX-XX-914026-1558513 // I don't want to match this line
130600298-110-528 // I want to match this line
Thanks in advance!
You have to add anchors:
^([A-Z0-9\-]{5,30})-[0-9]{1,10}-[0-9]{3}$
^ ^
Which mean start of line (^) and end of line ($).
If you don't, the match will be:
XXX-XX-914026-1558513
^^^^^^^^^^^^^^^^^
Also, you don't have to escape the caret in the end of a character class and you can use \d instead of [0-9]note: this will match numbers in any charset which gives:
^([A-Z0-9-]{5,30})-\d{1,10}-\d{3}$
This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 7 years ago.
I was trying to understand the following regex
^(?!\s)(?!.*\s\s)[ A-Za-z0-9'-]{1,35}(?<!\s)$
Here I understood,
^(?!\s) - (lookahead) means will not match if there is a leading space.
[ A-Za-z0-9'-]{1,35} - will allow A to Z, a to z, 0 to 9, (space), ' and -.
(?<!\s)$ - means will not match if there is a trailing space.
I am unable to understand what this sub-expression is doing?
(?!.*\s\s)
(?!.*\s\s)
means that in the string there should not be 2 adjacent spaces.
.* will scan the string.
\s\s will look for adjacent spaces.?! is negative lookahead so it will break when it finds such a string.See demo.
https://regex101.com/r/eB8xU8/4
I think there is no effect for this lookup.. It will return always true because here you are using greedy search.
It should be
(?!.*?\s\s): which means no two spaces continuous..
.* this is for match everything situation with out looking here and there
.* ? this makes this as a lazy operator and consider situation written at the right side of .*?
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Regular expression to match string not containing a word?
To not match a set of characters I would use e.g. [^\"\\r\\n]*
Now I want to not match a fixed character set, e.g. "|="
In other words, I want to match: ( not ", not \r, not \n, and not |= ).
EDIT: I am trying to modify the regex for parsing data separated with delimiters. The single-delimiter solution I got form a CSV parser, but now I want to expand it to include multi-character delimiters. I do not think lookaheads will work, because I want to consume, not just assert and discard, the matching characters.
I figured it out, it should be: ((?![\"\\r\\n]|[|][=]).)*
The full regex, modified from the CSV parser link in the original post, will be: ((?<field>((?![\"\\r\\n]|[|][=]).)*)|\"(?<field>([^\"]|\"\")*)\")([|][=]|(?<rowbreak>\\r\\n|\\n|$))
This will match any amount of characters of ( not ", not \r, not \n, and not |= ), or a quoted string, followed by ( "|=" or end of line )