Python Regex Match Before Character AND Ignore White Space

2024/9/8 9:23:45

I'm trying to write a regex to match part of a string that comes before '/' but also ignores any leading or trailing white space within the match.

So far I've got ^[^\/]* which matches everything before the '/' but I can't figure out how to ignore the white space.

      123 / some text 123

should yield

123

and

     a test / some text 123

should yield

a test
Answer

That's a little bit tricky. You first start matching from a non-whitespace character then continue matching slowly but surely up to the position that is immediately followed by an optional number of spaces and a slash mark:

\S.*?(?= *\/)

See live demo here

If slash mark could be the first non-whitespace character in input string then replace \S with [^\s\/]:

[^\s\/].*?(?= *\/)
https://en.xdnf.cn/q/73056.html

Related Q&A

Python Twisted integration with Cmd module

I like Pythons Twisted and Cmd. I want to use them together.I got some things working, but so far I havent figured out how to make tab-completion work, because I dont see how to receive tab keypres ev…

Read .pptx file from s3

I try to open a .pptx from Amazon S3 and read it using the python-pptx library. This is the code: from pptx import Presentation import boto3 s3 = boto3.resource(s3)obj=s3.Object(bucket,key) body = obj.…

PIL image display error It looks like the image was moved or renamed

Here is a bit of my code:from PIL import Imageimage = Image.open(fall-foliage-1740841_640.jpg) image.show()The error is when the default photo viewer is started and shows the error "It looks like …

How to delete numpy nan from a list of strings in Python?

I have a list of strings x = [A, B, nan, D]and want to remove the nan.I tried:x = x[~numpy.isnan(x)]But that only works if it contains numbers. How do we solve this for strings in Python 3+?

Pasting data into a pandas dataframe

This is the same question as this question, which is marked as a duplicate of this one. The problem, and the reason Im still asking, is that the solution provided (using pandas.read_clipboard()) curren…

Add graph description under graph in pylab [duplicate]

This question already has answers here:Closed 11 years ago.Possible Duplicate:Is there a way of drawing a caption box in matplotlib Is it possible to add graph description under graph in pylab?Lets s…

Using Python textwrap.shorten for string but with bytes width

Id like to shorten a string using textwrap.shorten or a function like it. The string can potentially have non-ASCII characters. Whats special here is that the maximal width is for the bytes encoding of…

How to create a transparent mask in opencv-python

I have sign (signs with arbitrary shape) images with white background and I want to get an image of the sign with transparent background. I have managed to create a mask and apply it to the image and t…

Variables with dynamic shape TensorFlow

I need to create a matrix in TensorFlow to store some values. The trick is the matrix has to support dynamic shape.I am trying to do the same I would do in numpy: myVar = tf.Variable(tf.zeros((x,y), va…

python protobuf cant deserialize message

Getting started with protobuf in python I face a strange issue:a simple message proto definition is:syntax = "proto3"; package test;message Message {string message = 1;string sender = 2; }gen…