Finding Correct Regular Expression

Shubham Debnath

what i am trying to find is the correct regular expression

import re

line = "The Boeing AH-64 Apache is an American four-blade,"

print(re.findall('(A.+)\s', line))

this is want i want

    ['AH-64', 'Apache' , 'American']

and this is what i'm getting

['AH-64 Apache is an American']
Wiktor Stribiżew

You may use a word boundary (\b) before A and then match one or more non-whitespace chars after it (\S+):

import re
line = "The Boeing AH-64 Apache is an American four-blade,"
print(re.findall(r'\bA\S+', line))

NOTE: to match A as a whole word, replace + (1 or more occurrences) with * (0 or more occurrences): r'\bA\S*'. I assume you want to match longer sequences though.

Or, since \S matches all symbols and punctuation, you may precise your regex a bit and use

print(re.findall(r'\bA[\w-]+', line))

where [\w-]+ matches 1 or more letter, digits, _ and - symbols.

See the Python demo showing ['AH-64', 'Apache', 'American'] output.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

What is the correct regular expression for finding specific pattern in these lines?

From Dev

regular expression for finding id

From Dev

regular expression for finding id

From Dev

Correct regular expression

From Dev

Correct regular expression

From Dev

Regular expression not finding both matches

From Dev

Regular expression for finding tag numbers in a list of cats

From Dev

Regular Expression for finding palindromes behaving strange

From Dev

Regular Expression Finding the Space before the Last word

From Dev

Finding the index of the second match of a regular expression in python

From Dev

python regular expression search not finding underscore in path

From Dev

Regular Expression for finding double characters in Bash

From Dev

preg_match correct regular expression

From Dev

Correct way to add comments to a Regular Expression in PHP

From Dev

Regular expression replace characters with the correct number of spaces

From Dev

looking for correct regular expression for pattern match in javascript

From Dev

Regular Expression does not accept correct inputs

From Dev

Please suggest correct regular expression for below situation

From Dev

Why my regular expression not correct in my data?

From Dev

Regular Expression Annotation Fails Despite Being Correct

From Dev

Regular Expression for check of correct amount of fields

From Dev

Regular expression: finding two elements not surrounding another element in text

From Dev

Regular expression for finding multiple patterns from a given string

From Dev

regular expression for finding a word position followed or preceded by special character

From Dev

C# regular expression for finding a certain pattern in a text

From Dev

Regular expression for finding more than 4 consecutive repeating characters in a string

From Dev

What would be regular expression of finding all strings starts with $ in java

From Dev

Regular expression - finding full words that contain a specific string

From Dev

What is the correct syntax for combining a regular expression stored in a variable with a quantifier?

Related Related

  1. 1

    What is the correct regular expression for finding specific pattern in these lines?

  2. 2

    regular expression for finding id

  3. 3

    regular expression for finding id

  4. 4

    Correct regular expression

  5. 5

    Correct regular expression

  6. 6

    Regular expression not finding both matches

  7. 7

    Regular expression for finding tag numbers in a list of cats

  8. 8

    Regular Expression for finding palindromes behaving strange

  9. 9

    Regular Expression Finding the Space before the Last word

  10. 10

    Finding the index of the second match of a regular expression in python

  11. 11

    python regular expression search not finding underscore in path

  12. 12

    Regular Expression for finding double characters in Bash

  13. 13

    preg_match correct regular expression

  14. 14

    Correct way to add comments to a Regular Expression in PHP

  15. 15

    Regular expression replace characters with the correct number of spaces

  16. 16

    looking for correct regular expression for pattern match in javascript

  17. 17

    Regular Expression does not accept correct inputs

  18. 18

    Please suggest correct regular expression for below situation

  19. 19

    Why my regular expression not correct in my data?

  20. 20

    Regular Expression Annotation Fails Despite Being Correct

  21. 21

    Regular Expression for check of correct amount of fields

  22. 22

    Regular expression: finding two elements not surrounding another element in text

  23. 23

    Regular expression for finding multiple patterns from a given string

  24. 24

    regular expression for finding a word position followed or preceded by special character

  25. 25

    C# regular expression for finding a certain pattern in a text

  26. 26

    Regular expression for finding more than 4 consecutive repeating characters in a string

  27. 27

    What would be regular expression of finding all strings starts with $ in java

  28. 28

    Regular expression - finding full words that contain a specific string

  29. 29

    What is the correct syntax for combining a regular expression stored in a variable with a quantifier?

HotTag

Archive