Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I'm looking for regex extract that would pull what I need from the following strings please:
2/A 100 House
to result in : 2/A 100
7/X 7 Capital Flat
to result in : 7/X 7
0/H 98 Kale Road
to result in : 0/H 98
The numbers and letter after the / can be anything so needing something more generic. I'm not too familiar with regex and I've only managed to extract everything before first occurrence of a letter using (.*?)\[A-Za-z]
I need to keep the first occurence of a letter and the following space and number but want rid of every other letter after that.
I'm coding in SQL.
Thanks for any help you can give!
Looking at your example, I'm assuming both the first character and the last one before the text you want to remove will be a digits. And the first character after / will be a letter
\d+\/\w+\s+\d+
See demo: https://regex101.com/r/cLGvkm/1
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
For example, I have the String "abcd", and I want all matches to be found in which at least 2 of those characters match, in the correct position. So, ab12, a1c2, 12cd, etc will all match because they contain at least 2 characters in the correct index from abcd.
I realize I could try doing it by /ab..|a.c.|a..d|.bc.|.b.d|..cd/g, but is there a better/simpler way to do this?
Thank you!!
You can easily accomplish this with the PyPi regex package.
See code working here
import regex
s = 'abcd'
a = ['ab12', 'a1c2', '12cd', '123d', 'abc4', 'abcd']
r = regex.compile('(?:'+regex.escape(s)+'){e<=2}')
for x in a:
if(r.fullmatch(x)):
print(x)
This uses fuzzy matching {e<=2} to identify strings that have 2 or fewer errors (insertion, substitution, deletion). You can instead specify {s<=2} for only substitutions if you'd like.
For list comprehension, you can replace the last three lines with the following:
print([x for x in a if(r.fullmatch(x))])
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
The program contains text of this type:
A B Ccccc
A Ccccc
ACcccc
ABCcccc
I need only such text to remain:
Ccccc
I wrote a replacement function, but I just can’t pick up a pattern
How to make such a pattern?
No need for regex, nor VBA. It seems you simply are looking for the position of the last upper-case letter and then to extract from there:
Formula in B1 (with Excel O365):
=MID(A1,MAX(SEQUENCE(LEN(A1))*(EXACT(UPPER(MID(A1,SEQUENCE(LEN(A1)),1)),MID(A1,SEQUENCE(LEN(A1)),1)))),LEN(A1))
If you don't have Excel O365:
=MID(A1,MAX(ROW(A1:INDEX(A:A,LEN(A1)))*(EXACT(UPPER(MID(A1,ROW(A1:INDEX(A:A,LEN(A1))),1)),MID(A1,ROW(A1:INDEX(A:A,LEN(A1))),1)))),LEN(A1))
You probably need to enter as array through: CtrlShiftEnter
If you must go through VBA and regex then a pattern like:
[A-Z][^A-Z]*$
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I have data as follows:
text = "hello there, a:123ijk^&45b: I am working on this regex a:45o#iu67b: I need to solve it"
I want to get only the string sequences between a: and b: . My output should be
position('a:123ijk^&45b:') and position('a:45o#iu67b:') . I tried a few regex patterns but it returns between first a: and last b:, so couldn't solve it. As an alternate I have a boring way of writing a loop based solution but want to avoid it.
Appreciate if anyone can can help with this
Use a lazy quantifier:
a:(.*?)b:
"I want to get only the string sequences between a: and b: "
Maybe like:
(?<=a:).+?(?=b:)
The ? in the middle makes the greedy + lazy and stops the match when :b is ahead the first time. This would now get the values that are actually in between
Edit:
"realized ... my question was also put wrong.. what if I want to include only a not b?"
Try this:
(?<=\s)a:.+?(?=b:)
I included an extra positive lookbehind to make sure the a: is preceded by a \s.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I am trying to filter medical words from general english words.
but most of the drugs name contains hypen in it.
pls suggest how to check in python, that a string contains only alphabets and hypen.
for example : anti-allergic
Simplest way to check string is as below, remove '-' from string and check if remaining characters are all alphabets.
test_str = 'anti-allergic'
if test_str.replace('-','').isalpha():
print('Valid string')
This can be accomplished by using regular expressions (https://docs.python.org/3/library/re.html), where a (very quick and dirty) regex could ask for all letters, a to z (and A to Z), that has a hyphen in it.
([a-zA-Z]+[-].+)
Would match the following:
suoad
ADDADA
waeewrw
omaeqweSADADSwu
iraaief
anti-allergic
ANTI-ALLERGIC
testtesttest
You can test this out yourself using https://pythex.org/.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I'm trying to build a regex to check whether a given number is included or not in a string containing numbers separated by comma. For eg
Check if 1 is in
'1,2,3,4' - ans yes
'2,13,4' - ans no
"1"- ans yes
How can this be done using regex.
Thanks in advance.
The word boundaries would be usefull
\b1\b
This ensures that the number is presceded and followed by a word boundary
\b assert position at a word boundary. , is included in the word boundaries
Regex Demo