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 `
Related
This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 2 years ago.
I am a newbie in regular expression, I have written regular expression for ${serviceName} basicly I want to take the words in between ${ } So I already wrote regular expression for this that is perfectly fine
"\\$\\{(\\w+)\\}"
But what I want to take any values not only the words which are in between ${serviceName.1.Type}.So can you guys help me with regular expression for ${serviceName.1.Type}.
I hope my question is clear.
Thanks In Advance.
A good place to test regular expressions is https://regex101.com/
\w+ matches any word character (equal to [a-zA-Z0-9_])
If you want to match anything you can replace it with: .*
.* matches any character (except for line terminators)
You might want to add a "?" at the end to match to first "}"
*? Quantifier — Matches between zero and unlimited times, as few times as possible, expanding as needed
Also you don't need to escape the { } in this case
So what you want is:
"\\${(.*?)}"
\$\{([\w?\.?\d?\s?]+)\}
This expression captures as a group everything that appears between {}
You can then call the group with the expression $1
On this web you will see your exercise solved and if other expressions have some additional character you can try to add it. Now it is prepared for points \. , spaces \s, letters \w and digits \d
This question already has answers here:
Regex Last occurrence?
(7 answers)
Closed 3 years ago.
I have the following RegEx syntax that will match the first date found.
([0-9]+)/([0-9]+)/([0-9]+)
However, I would like to start from the end of the content and search backwards. In other words, in the below example, my syntax will always match the first date, but I want it to match the last instead.
Some Text here
01/02/15
Some additional
text here.
10/04/14
Ending text
here
I believe this is possible by using a negative lookahead, but all my attempts failed at this because I don't understand RegEx enough. Help would be appreciated.
Note: my application uses RegEx PCRP.
You could make the dot match a newline using for example an inline modifier (?s) and match until the end of the string.
Then make use of backtracking until the last occurrence of the date like pattern and precede the first digit with a word boundary.
Use \K to forget what was matched and match the date like pattern.
^(?s).*\b\K[0-9]+/[0-9]+/[0-9]+
Regex demo
Note that the pattern is a very broad match and does not validate a date itself.
This question already has answers here:
Regex: match everything but a specific pattern
(6 answers)
Closed 4 years ago.
After countless hours of trying to get this regex to work (including looking all over StackOverflow), I thought I'd reach out for help on here as I have not been successful).
I have tried creating a regex to match everything and to not match any parameters that look like this:
text=3242ffs3F34
The data after the = sign can be random (its a mixture of numeric and string characters) and is never the same. So far I have created the following regex below, which is almost doing what I am after but it does not work.
\b(?!text=.*)\b\S+
Assistance is much appreciated!
EDIT:
I will be using the regex to match everything in a file but to filter out all parameters that look like this:
text=3242ffs3F34
Below is an example of how the config file will look like:
This is a test
test=asda
test2=22rr2
text=3242ffs3F34
test5=hello
To match everything except strings containing LAST_DOMINO_TIME= as substring you can use the expression:
(?!.*\bLAST_DOMINO_TIME=.*$)^.*$
(?! Negative lookahead.
.* Match anything.
\b Word boundary.
LAST_DOMINO_TIME= Literal substring.
.*$ Anything up to end of string.
) Close lookahead.
^.*$ Assert position beginning of line, match anything up to end of line.
You can try it here.
This question already has answers here:
Regex plus vs star difference? [duplicate]
(9 answers)
RegEx match open tags except XHTML self-contained tags
(35 answers)
Closed 4 years ago.
I am trying to match a string "menu-item" but has a digit after it.
<li id="menu-item-578" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-578">
i can use this regex
menu-item-[0-9]*
however it matches all the menu-item string, i want to only match the "menu-item-578" but not id="menu-item-578"
how can i do it?
thank you
You should avoid using menu-item-[0-9]* not because it matches the same expected substring superfluously but for the reason that it goes beyond that too like matching menu-item- in menu-item-one.
Besides replacing quantifier with +, you have to look if preceding character is not a non-whitespace character:
(?<!\S)menu-item-[0-9]+(?=["' ])
or if your regex flavor doesn't support lookarounds you may want to do this which may not be precise either:
[ ]menu-item-[0-9]+
You may also consider following characters using a more strict pattern:
[ ]menu-item-[0-9]+["' ]
Try it works too:
(\s)(menu-item-)\d+
https://regex101.com/
\s Any whitespace character
Use a space before, like this:
\ menu-item-[0-9]*
The first ocurrence has an " right before, while the second one has a space.
EDIT: use an online regex editor (like Regex tester to try this things.
This question already has an answer here:
Finding the indexes of multiple/overlapping matching substrings
(1 answer)
Closed 7 years ago.
I'm trying to find all matches of a particular pattern "8ab|ab8" in the string "8ab8". So I tried the R command gregexpr("8ab|ab8","8ab8") hoping to get a return vector with the starting positions as c(1,2).
Unfortunately, it seems that what happens is that once the first pattern is matched, that portion of the string is "removed" and the second pattern won't be matched.
For example, once "8ab" is matched, "8ab8" becomes "8" and when R tries matching "ab8" in "8", the pattern won't be found. I know this because gregexpr("8ab|ab8","8ab ab8") works fine and returns starting positions of pattern matches as c(1,5).
The question is, how do I match the same pattern multiple times in the first case?
Use perl regular expressions: perl=TRUE . (see ?regex for info on perl regular expressions)
gregexpr("(?=8ab)|(?=ab8)","8ab8",perl=T)