What's going on in this substitution? [closed] - regex

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
Could I get some help figuring out what's going on?
How does the .xnext end up at the beginning of the string?
[237] > cat /tmp/text.txt
xtop.xnext|sig 12345
[238] > perl -p -e 's/\.xnext\\|/.xnext./;' /tmp/text.txt
.xnext.xtop.xnext|sig 12345

In your regex, | is the "alternation" metacharacter and not a literal pipe character. So your pattern
\.xnext\\|
can either match the literal string .xnext\, which is what is specified on the left side of the alternation character, or
nothing, which is what is specified to the right side of the alternation.
So the beginning of your input string is a match for your regular expression pattern, and your substitution pattern .xnext. is prepended to your string.
The pattern you wanted to use was
\.xnext\|
which is how to specify the literal string .xnext|.

Related

regex: Check the first character AND match [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I'm working on a regular expression email validation assignment and it is working pretty well. I can match based on all the criteria I'm asked to. Here is my current pattern:
^([a-zA-Z0-9.\-\_\+]+)#([a-zA-Z0-9.\-\_\+]+)$
I also want to check that it begins with an alphabetic character. So I changed my pattern to this:
^[a-zA-Z]([a-zA-Z0-9.\-\_\+]+)#([a-zA-Z0-9.\-\_\+]+)$
And that works fine and only produces the match when the first character is alphabetic. BUT, the matches that are produced cut off the first character. So for example, if I try:
Billy#bobby.com
The match for the user-id before the # sign is coming back as "illy". I want it to come back as "Billy"
See the regex in action here: https://pythex.org/?regex=%5E%5Ba-zA-Z%5D(%5Ba-zA-Z0-9.%5C-%5C_%5C%2B%5D%2B)%40(%5Ba-zA-Z0-9.%5C-%5C_%5C%2B%5D%2B)%24&test_string=b2ill..%2B..DSD_y.23%40bobby.com&ignorecase=0&multiline=0&dotall=0&verbose=0
Nick gave me the help I needed. Here was my final pattern
^([a-zA-Z][a-zA-Z0-9.-_+]+)#([a-zA-Z0-9.-_+]+[a-zA-Z])$

My regular expression matches unexpected patterns [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
would anyone know why is the second pattern displayed it should not match the regex...
$grep [[:digit:]{4}] file
99g66
9888
$
Because the englobing [] will escape your {4} part.
The correct way of achieving this is the following
[[:digit:]]{4}
Your expression [[:digit:]{4}] is a character set that matches either a digit, a {, or }. You probably meant [[:digit:]]{4}. Use that regular expression in extended mode with grep -E or egrep or escape the braces when using basic regular expression mode:
$ egrep '[[:digit:]]{4}' file
$ grep '[[:digit:]]\{4\}' file # braces escaped

Why does this regex pattern fail to match the string [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I am trying to match a date string, and have tested out my pattern on regex101. I think I am following the regex rules, but I'm obviously missing something, and the pattern is not matching the string.
My regex pattern is: \s?(Mon|Tue|Wed|Thurs|Fri)day\s\d{0,2}(st|nd|rd|th)\s (January|February|March|April|May|June|July|August|September|October|November|December)\,\s\d{4}
The string I'm trying to match is:
Monday 16th October, 2017
Which can appear in the document with or without lead/trailing whitespace(s).
Why is the pattern not matching?
By copying your regex from your post, I saw there's a redundant space here:
(st|nd|rd|th)\s (January|
↑
I'm not sure if it's a formation problem or not. Anyway, remove it and you should be fine.
Suggestion:
Depending on the language you're using (was tagged Python), use a library that parses the string for you, instead of having this (ugly) regex.

Perl matching string with mix of alphanumeric,_ and any number of square braces [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
How do I match a string with " alphanumeric characters, underscore and any number of open and closed square braces ".
Example : " CDN_MBIT_hresp_s_reg[0]_MB_hresp_s_reg[1]bbjabs_chiansmokrs[6] "
I tried $line=~/[a-zA-Z0-9_/[/]]/;
This seems doesn't work.
P.S. This question is quite similar to Regex Matching Square Brackets
but not same
Thank you in advance.
Wrong slash used for escaping.
/[a-zA-Z0-9_\[\]]/
Alternatively, you could simply use
/[\w\[\]]/
Both of those match exactly one character. If you wanted to capture the string, you'd want
/([\w\[\]]+)/

Notepad++ and regex [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I am doing some test with Find in Notepad++ using regex. Here is my problem that I can't figure out why:
My text is:
abc/xyz/p234/s-sdf
The following regex matches well:
[a-z]+/[a-z]+(/p[0-9]+)/s-[a-z]+
But why the following regex (with an added '?') does not match anymore:
[a-z]+/[a-z]+(/p[0-9]+)?/s-[a-z]+
Am I right that in the last regex, the '?' means that (/p[0-9]+) can appear zero or one time?