Given:
<currency: word with spaces, 56>
and the regular expression:
<(?:CURRENCY):[ ]*(\w+(\s*,\s*)\d+(\s*\d+)*)>
What must I change to accept spaces in the "words with spaces"
You're currently searching for \w which is a word character, the opposite of whitespace. Also, not sure if you're intending to capture a whitespace and commas, instead of the number values. This captures only the word and the numbers.
<CURRENCY:\s+?(.+)\s*,\s*(\d+)(?:\s*(\d+))?>
I find regex101.com to be helpful when debugging these things.
does this help?
<(?:currency)\:\s*\w(\w|\s)+,\s*\d+(\s*|\d+)*>
This should do:
<currency: ((\w+\s*)*), (\d+)>
another one:
<(?:currency:)\s([\w\s]*),\s\d+>
if don't want to capture empty string, change * to +
Also you don't need the non capturing group ?:
<currency:\s([\w\s]*),\s\d+>
would do the same.
This worked for me:
<(currency):[ ]*(\w+(\s+\w+)*\s*,\s*\d+(\s*\d+)*)>
Not sure what "?:" does, so you may want:
<(?:CURRENCY):[ ]*(\w+(\s+\w+)*\s*,\s*\d+(\s*\d+)*)>
Related
I have a set of words coming in one by one like aa, ##, ???, ~~~, ?~ etc
I need a regex to find if any of these words is containing only ? or only ~.
Of the above input examples, ??? and ~~~ should match but not the others.
I tried ^[\s?]*$ and ^[\s~]*$ separately and it works, I am trying to combine them.
^[\s?||~]*$ doesn't work as it also recognizes ?~ as valid.
Any help?
You can use this regex, which looks for a string starting with a ~ or a ?, and then asserts that every other character in the string is the same as the first one using a backreference (\1):
^([~?])\1+$
Demo on regex101
You need to use backreference to achived your desired result.
If you want only ~ or ? use
^([~?])\1+$
If you want any repetitive pattern, use
^(.)\1+$
Explanation (.) or ([~?]) capturing the first charactor.
Then, \1+ checking the first charactor, one or more times (backreferencing)
You want to match lines that both start and end with any number of either a tilde or questionmark. That would be ^\(~\|?\)*$. The parentheses to make a group and the vertical bar to do the 'or' need to be backslash escaped.
Let's say I have the text a123456. I want a string of b123456 to match. So essentially, 'match if all characters are the same except for the first character'. Am I asking for the impossible with regex?
Use the dot (.) to match any character. So, a possible Regex would be:
/^.123456$/
If you want to use zero length assertion with regex, you can have lookbehind approach in following way :
(?<=\w)your_value$ // your_value should be text which you want to check
I think you can figure it out on your own. This ain't tough, just needs some understanding between you and Regex. Why don't you go through the following links and try to make a regex on your own.
https://www.talentcookie.com/2015/07/regular-expressions/
https://www.talentcookie.com/2015/07/lets-practice-regular-expression/
https://www.talentcookie.com/2016/01/some-useful-regular-expression-terminologies/
I have the following regexp that matches underscores, letters, numbers, and spaces. My problem is that I don't want to match strings with only spaces. Any help? Thanks!
[a-zA-Z0-9_\\s]+
If the first character must be a non-space then this will do:
[a-zA-Z0-9_][a-zA-Z0-9_\s]*
If you need to be able to match leading spaces as well:
\s*[a-zA-Z0-9_][a-zA-Z0-9_\s]*
[a-zA-Z0-9_\\s]*[a-zA-Z0-9_]+[a-zA-Z0-9\\s]*
There are more elegant ways to phrase this, but I don't know if your tool of choice supports it.
I have been struggle to write regex that matches words longer than a given length within parentheses. First I thought I could do this with \(\w{a,}\) but I realize that it doesn't match with words with white space (ab cd ef). All I want to do is find out any characters within parentheses longer than, for instance, 3 characters. How can I resolve this problem ?
What is a word with white space?
if you want to match any character then use .
\(.{3,}\)
. matches any character except newlines
But be careful, this is greedy. it will match for example also
(a)123(b)
To avoid this you could do something like
\([^)]{3,}\)
See it here online on Regexr
[^)] means any character except a )
You could use a character class that includes both \w and \s:
\([\w\s]{a,}\)
Maybe do you mean?
\([\w\s]{a,}\)
if it has a space in it it's not a word anymore.
is matching any characters fine \(.{a,}\)? Or you just need the whitespace \(\(\w|\s\){a,}\)?
How can it be that this regular expression also returns strings that have a _ underscore as their last character?
It should only return strings with alphabetical characters, mixed lower- and uppercase.
However, the regular expression returns: 'action_'
$regEx = '/^([a-zA-Z])[a-zA-Z]*[\S]$|^([a-zA-Z])*[\S]$|^[a-zA-Z]*[\S]$/';
Because \S means "not whitespace character", \S matches _
A group should not have an underscore though, so, if you meant that, it could be that you are getting the whole match back and not just the first group.
Please show how are you using the regex to clarify that, if needed.
The [\S] will match everything that is not whitespace, including underscore.
Also, your expression is very odd!
If you want a string that only contains letters, then use ^[a-zA-Z]*$ or ^[a-zA-Z]+$ (depending on if blank is allowed or not).
If you're trying to do something else, you will need to expand on what that is.
\S matches any non-whitespace char - thus _
You should show the text and what part from you want to extract from it.
Regular expression shouldn't be so big like yours.
Work on small expression batches... At this size, is very difficult to help you.